Running a global test setup before each test in Jest
You could use setupFilesAfterEnv
(which replaces setupTestFrameworkScriptFile
, deprecated from jest version 24.x) which will run before each test
// package.json
{
// ...
"jest": {
"setupFilesAfterEnv": ["<rootDir>/setupTests.js"]
}
}
And in setupTests.js
, you can directly write:
global.beforeEach(() => {
...
});
global.afterEach(() => {
...
});
Just as a follow-up to the answer from @zzz, the more recent documentation on Configuring Jest notes:
Note:
setupTestFrameworkScriptFile
is deprecated in favor ofsetupFilesAfterEnv
.
So now, your file should look like this:
// package.json
{
// ...
"jest": {
"setupFilesAfterEnv": [
"<rootDir>/setupTests.js"
]
}
}