Jest Mock User Module in All Test Files
Jest documentation has been updated and uses setupFilesAfterEnv
to run code that can utilize Jest functions like jest.mock()
.
Updating @smirky's example to use setupFilesAfterEnv
:
// package.json
...
"jest": {
"setupFilesAfterEnv": ["./src/setupTests.js"]
}
// setupTests.js
jest.mock('config'); // Path to config file
On a sidenote, if you wish to import/require
modules for every test you run, you can use setupFiles
.
You can add setupFiles
or setupFilesAfterEnv
into your jest configuration to run before all your tests.
https://jestjs.io/docs/configuration#setupfiles-array
// package.json
...
"jest": {
"setupFiles": "./src/setupTests.js"
}
// setupTests.js
jest.mock('config'); // Path to config file