-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expose matchers in expect.extend #10329
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
Comments
I ended up here from #2547, and @SimenB you'd asked for use cases in that one (...admittedly ~3 years ago :-D), but similar to @georeith I want to make a custom matcher that a) accepts args, b) does some pre-processing, and then c) hands off to an existing matcher, in my case Basically, in our project, the In my case I'm using a export async function toMatchEntity<T>(actual: Entity, expected: MatchedEntity<T>): Promise<CustomMatcherResult> {
// Clean up `actual` to be "just data"
const copy = ...project specific stuff...
// Blatantly grab `toMatchObject` from the guts of expect
const { getMatchers } = require("expect/build/jestMatchersObject");
// Now use `toMatchObject` but with our "just data" version of `actual`
return getMatchers().toMatchObject.call(this, copy, expected);
} With @georeith 's proposal, the return expect.extend({
toMatchEntity(actual, expected) {
const copy = ...same clean up...;
return this.matchers.toMatchObject(copy, expected);
},
}); |
@stephenh This works great, I have been using a similar code for some time now and it's 💯 . However recently I have tried to do the same with |
@bpinto hm, no, I haven't tried to re-use |
Another vote for the core matchers to be exposed for use within custom matchers. In my use case I'd like to write a db-based custom matcher Internally this would be import { toMatchObject } from 'somewhere'
export const toHaveBeenUpdatedTo = async (original, match) => {
const updated = await getUpdatedFromDatabase(original)
return toMatchObject(updated, match)
} |
that would be extremely helpful to be able to re-use existing matchers in custom matchers. |
This no longer works, and breaks with the error message:
|
See this PR: #13375 |
This should be merged then ;) |
@mrazauskas until the PR is merged, I'd like to understand why the import does not work anymore. The file is there, why can't it be resolved? |
Perhaps newer version of Node is taking into account |
I also need this, I found these hacks to be working const { matchers } = globalThis[Symbol.for('$$jest-matchers-object')];
// or
import matchers from 'expect/build/matchers';
// or
const matchers = require('expect/build/matchers').default; |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
🚀 Feature Proposal
Expose existing matchers inside
expect.extend
.Motivation
Sometimes you want the existing functionality of a matcher but you want to it to transform the input before doing so, for instance, to ignore some specific keys of an object.
Writing a custom matcher is extremely verbose and requires importing additional packages to maintain the same quality of the core matchers (diff in messages).
For example, if I want a matcher that performs
toEqual
on two objects but ignores a single property on those objects:Example
and then:
Pitch
I am aware this has been asked for before:
The response was to use
expect.extend
and I do not think it considers these cases where usingexpect.extend
as it stands is not only massively inconvenient upfront for such a simple comparison but creates longer term debt having to maintain the matcher, whereas leveraging the return value of a core matcher allows your matcher to benefit from the continued maintenance of it in the jest core, e.g., if it gets improved messages or the already very verbose matcher return API changes.This proposal is to enable the ability to write matchers that don't want to introduce new matching behaviour but want to transform their inputs before matching.
Other alternatives include:
You then have to handle
not
yourself by either making separate functions or flagging it:Which will work, but now requires you to know an entirely different syntax because of a slight difference to the matcher.
The text was updated successfully, but these errors were encountered: