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(tests): clean up jest.Mock usage in tests #13238

Merged
merged 2 commits into from Sep 10, 2022

Conversation

mrazauskas
Copy link
Contributor

@mrazauskas mrazauskas commented Sep 10, 2022

Following up #13235

Summary

Cleaning up jest.Mock usage in tests. The type was unnecessary or used to manually reset a mock function (jest.clearAllMocks() seemed like better idea). Otherwise I used generic type arguments to have it typed it properly. Also fixed other type errors in these tests and a couple of typos caught by cspell.

This should help migrating to build-in Jest types and to setup typecheck for test files one day.

Test plan

Green CI.

@@ -652,7 +658,7 @@ const createSpy = (fn: jest.Mock) => {

test('incomplete recursive calls are handled properly', () => {
// sums up all integers from 0 -> value, using recursion
const fn: jest.Mock = jest.fn(value => {
const fn: jest.Mock<number, [value: number]> = jest.fn(value => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Otherwise the default is jest.Mock<any, any>.

Comment on lines -21 to -23
const cases: Record<string, jest.Mock> = {
fancyCondition: jest.fn(path => path.length > 10),
testRegex: jest.fn(path => /.test.js$/.test(path)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

jest.fn() does nothing here. Can be simplified.

Comment on lines +27 to +30
afterEach(() => {
jest.clearAllMocks();
jest.resetModules();
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tricky one. If I got it right: fake was being reset manually, looks like jest.clearAllMocks() does the same job; also require in beforeEach above has no effect without jest.resetModules() because of module caching. Or?

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! 👍

@SimenB SimenB merged commit 0aa9b02 into jestjs:main Sep 10, 2022
@mrazauskas mrazauskas deleted the fix-jest.Mock-usage branch September 10, 2022 08:49
@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