-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Extended toEqual to allow custom equality matcher. #7352
Extended toEqual to allow custom equality matcher. #7352
Conversation
* Modified toEqual to permit array of custom equality matchers. * Added unit test with apprixmate numeric matching. * Updated expect API documentation.
Hey @EthanRBrown thanks for contributing to Jest! Is there a proposal issue for this or is this PR the proposal? |
@rickhanlonii Hi, RIck...I was a little confused about the process, I apologize. It's my first time contributing to Jest. I did my best to follow Anyway, I'm happy to follow whatever process, just new to it. I have a need, I have a contribution that will solve that, I hope other people find it useful, etc. |
Um, in this case I think the custom matcher would be better solution? You can create on locally or use it in every file, here's the docs on this: https://jestjs.io/docs/en/expect#expectextendmatchers |
@thymikee That was my first approach, but because Jest's |
No sweat, we can discuss here! Did you know that you can use the expect.extend({
toEqual(received, expected) {
const pass = this.equals(received, expected, [customNumericMatcher]);
// return message
},
}); |
@rickhanlonii I was not aware...and I was looking for something like that, unsuccessful. This certainly solves my problem...happy to close this PR if you think this is an inappropriate extension of the Jest expect API. |
If there's something that could ease finding this information on our docs, please send a PR to the docs :) |
While that example is useful, @rickhanlonii, extending expect.extend({
toEqual(received, expected, extraMatchers = []) {
const pass = this.equals(received, expected, extraMatchers)
// Duplicated from jest.
// https://github.com/facebook/jest/blob/f3dab7/packages/expect
// /src/matchers.ts#L538-L569
/* eslint-disable */
const matcherName = 'toEqual'
const options = {
comment: 'deep equality',
isNot: this.isNot,
promise: this.promise,
}
const message = pass
? () =>
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
`Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`
: () => {
const difference = diff(expected, received, {expand: this.expand});
return (
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
(difference && difference.includes('- Expect')
? `Difference:\n\n${difference}`
: `Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`)
);
};
return {
actual: received,
expected,
message,
name: matcherName,
pass,
}
/* eslint-enable */
// Duplication ends here.
},
}) |
The proposing solution will be a lot more easier than implementing many extend methods in my case. I used MomentJs and I want to use the isSame method from the moment object as the equality matcher. The Extend is not convenient because I have to implement all yours expect method that use equal like toEqual, toHaveBeenCalledWith, toHaveBeenNthCalledWith, etc. Instead, I could set a custom matcher for Moment object to use in all Jest equal call. Code suggestion
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
The internal implementation of
toEqual
allows for custom equality matchers. Jest takes advantage of this currently for iterable equality, but it would be very useful for end users to have access to this same extensibility. In particular, my case is that I want to be able to combine the functionality oftoBeCloseTo
withtoEqual
. That is, I have test cases with large, deep objects with floating-point values I wish to compare approximately. Exposing the existing custom equality matcher functionality allows this and other useful extensions.Test plan
All existing tests pass, and I added a unit test to demonstrate the effective use of custom equality matchers to do approximate numeric comparison. Specifically:
Example of undesirable failing test:
Using custom numeric matchers to create passing test with approximate numeric comparison: