https://github.com/kentcdodds/how-jest-mocking-works
https://github.com/kentcdodds/how-jest-mocking-works
kcd-edu
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/kentcdodds/how-jest-mocking-works
- Owner: kentcdodds
- Created: 2016-11-07T20:10:53.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2021-01-25T23:14:16.000Z (over 4 years ago)
- Last Synced: 2025-04-02T11:49:53.035Z (6 months ago)
- Topics: kcd-edu
- Language: JavaScript
- Size: 997 KB
- Stars: 311
- Watchers: 6
- Forks: 32
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - how-jest-mocking-works
README
# How Jest Mocking Works
This title is a bit misleading. I'm not explaining everything about Jest mocking, just something I wanted to test out
and thought was kind of interesting. So it turns out that if you make a call to `jest.mock` in the root of your module,
that mocking will take place before any require statements are resolved/run. This actually makes Jest's mocking
capabilities really powerful! It essentially means that you can import all the stuff you would normally, and not worry
about whether you're mocking things in time. Pretty legit!And Christoph [comes in](https://twitter.com/cpojer/status/795729860819820544) to give a little more insight here :)
Looks like you can use `jest.doMock` or `jest.dontMock` to change this behavior.