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

Add support for jest.clearAllMocks and jest.resetAllMocks #19

Merged
merged 3 commits into from
Apr 24, 2022

Conversation

vajahath
Copy link
Contributor

@vajahath vajahath commented Apr 18, 2022

This PR aims to add support for jest.clearAllMocks and jest.resetAllMocks.
Fixes #18

  • Show proper error message instead of the existing error log TypeError: Cannot read properties of undefined (reading 'length'). Now shows TypeError: jest.clearAllMocks is not a function. Fixed in 14336c3
  • Support jest.clearAllMocks
  • Support jest.resetAllMocks
  • Tested

Need a small help with last 2 tasks

Thanks for hinting me where to change. I tried this, and I'm facing a small issue.

I could have done like this in the global.setup.js:

// excerpt
globalThis.jest = {
  fn: mock.fn,
  spyOn: mock.spyOn,
  clearAllMocks: mock.JestMock.clearAllMocks.bind(mock.JestMock),
  // ...
};

But the jest-mock package seem not exporting the JestMock instance. So I can't do mock.JestMock.<thing>.
https://github.com/facebook/jest/blob/d2f3dc694d72c0208f47fc2e2ca101e26a9a6d68/packages/jest-mock/src/index.ts#L1239-L1243

Could someone help?

@nicolo-ribaudo
Copy link
Owner

nicolo-ribaudo commented Apr 18, 2022

We could instantiate ModuleMocker ourselves and use it to grab also fn and spyOn.

@nicolo-ribaudo
Copy link
Owner

nicolo-ribaudo commented Apr 19, 2022

@vajahath This looks good; let me know when you have verified that it works in your app! (we don't have unit tests yet 😬)

@vajahath vajahath marked this pull request as ready for review April 20, 2022 06:19
@vajahath
Copy link
Contributor Author

vajahath commented Apr 20, 2022

Yes I just verified with my couple of projects.

@nicolo-ribaudo
Copy link
Owner

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit e13bf7e into nicolo-ribaudo:main Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support .jest.clearAllMocks()
2 participants