Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Custom static matchers #5490

Closed
aymericbouzy opened this issue Feb 7, 2018 · 3 comments
Closed

[feature] Custom static matchers #5490

aymericbouzy opened this issue Feb 7, 2018 · 3 comments

Comments

@aymericbouzy
Copy link
Contributor

Do you want to request a feature or report a bug?

feature

My need

I'm trying to assert that an object has 2 keys where values are Date-like objects at a specific time (in this case, using moment).

This is my best solution at the moment:

const period = {
  start: moment("2018-05-17"),
  end: moment("2018-05-18"),
}
expect(period).toEqual({
  start: expect.any(Object),
  end: expect.any(Object),
})
expect(period.start.isSame(moment("2018-05-17"))).toBe(true)
expect(period.end.isSame(moment("2018-05-18"))).toBe(true)

What I would like my code to look like

const period = {
  start: moment("2018-05-17"),
  end: moment("2018-05-18"),
}
expect(period).toEqual({
  start: expect.sameDate("2018-05-17"),
  end: expect.sameDate("2018-05-18"),
})

Suggested API

It is already possible to extend matchers in this way :

expect.extend({
  sameDate(received, expected) {
    ...
  },
})
expect(period.start).sameDate("2018-05-17")

If it could also add the method sameDate to the expect object, everything would work out for me 🎉

PS: thanks for all the amazing work ❤️

@SimenB
Copy link
Member

SimenB commented Feb 7, 2018

Duplicate of #4711. PR welcome!

@SimenB SimenB closed this as completed Feb 7, 2018
@aymericbouzy
Copy link
Contributor Author

@SimenB I've not contributed to the repo yet : do you feel like it's a good first task?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants