-
-
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
fix(jest-snapshot): fix typings of snapshot matchers #13240
Conversation
- `[expect]` Expose `ExpectationResult` type ([#13240])(https://github.com/facebook/jest/pull/13240)) | ||
- `[jest-snapshot]` Expose `Context` type ([#13240])(https://github.com/facebook/jest/pull/13240)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used those in type tests, but in a way they can be useful to defined matcher functions (ExpectationResult
) or to implement snapshot matchers (Context
).
beforeEach(() => { | ||
throwMatcher = toThrowErrorMatchingSnapshot.bind({ | ||
snapshotState: {match: matchFn}, | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.bind
was reworked into .call
, which proofed to be friendlier with types. Also mocks are cleared in afterEach
calling jest.clearAllMocks()
.
|
||
expect(matchFn).toHaveBeenCalledWith( | ||
expect.objectContaining({received: 'coconut', testName: ''}), | ||
expect(mockedMatch).toBeCalledTimes(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be sure that mock was cleared.
export const toMatchSnapshot: MatcherFunctionWithContext< | ||
Context, | ||
[propertiesOrHint?: object | string, hint?: string] | ||
> = function (received, propertiesOrHint, hint) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and bellow – types in function signature is the only change. The rest is white space / formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
Actually this particular bug could be caught by typechecking the test files. I will try to set this up. |
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. |
Split from #13238
Summary
While reworking
jest.Mock
usage in tests ofjest-snapshot
package, I noticed that types of matchers are incorrect. To be preciseMatcherFunctionWithContext
was not used correctly and because of that matchers imported into tests had wrong types. Ups.. I introduced this some time ago.Fixed everything and added type tests to prevent regression in the future.
Also cleaned up
jest.Mock
usage.Test plan
Green CI.