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

fix(jest-snapshot): fix typings of snapshot matchers #13240

Merged
merged 5 commits into from Sep 10, 2022

Conversation

mrazauskas
Copy link
Contributor

Split from #13238

Summary

While reworking jest.Mock usage in tests of jest-snapshot package, I noticed that types of matchers are incorrect. To be precise MatcherFunctionWithContext 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.

Comment on lines +6 to +7
- `[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))
Copy link
Contributor Author

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).

Comment on lines -40 to -44
beforeEach(() => {
throwMatcher = toThrowErrorMatchingSnapshot.bind({
snapshotState: {match: matchFn},
});
});
Copy link
Contributor Author

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);
Copy link
Contributor Author

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.

Comment on lines +156 to +159
export const toMatchSnapshot: MatcherFunctionWithContext<
Context,
[propertiesOrHint?: object | string, hint?: string]
> = function (received, propertiesOrHint, hint) {
Copy link
Contributor Author

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.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@mrazauskas
Copy link
Contributor Author

Actually this particular bug could be caught by typechecking the test files. I will try to set this up.

@SimenB SimenB merged commit 63f8a99 into jestjs:main Sep 10, 2022
@mrazauskas mrazauskas deleted the fix-jest.Mock-usage branch September 10, 2022 11:46
@github-actions
Copy link

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.
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 Oct 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants