globalSetup is executed in different context than tests
See original GitHub issueCurrently if globalSetup
is used to asynchronously prepare something, and then the same file is imported in tests it results in two separate module instances.
Repo to reproduce: https://github.com/kirill-konshin/jest-globalsetup-bug
// jest.globalSetup.js
const doSomethingAsync = () => new Promise(resolve => resolve(Math.random()));
let browser;
const globalSetup = () => doSomethingAsync().then((res) => {
browser = res;
console.log('Setup', module.exports.getBrowser());
});
module.exports = globalSetup;
module.exports.getBrowser = () => browser;
// jest.globalTeardown.js
const setup = require('./globalSetup');
module.exports = () => {
console.log('Teardown', setup.getBrowser());
};
// one of test files
const setup = require('./globalSetup');
describe('browser test', () => {
it('opens a page', async () => {
console.log('Test', setup.getBrowser());
});
});
After npm test
console shows that test had it undefined
while setup
and teardown
both printed same value:
> jest-bug@1.0.0 test /Users/dis/Sites/jest-bug
> NODE_ENV=test jest
Determining test suites to run...
Setup 0.23915022164799082
PASS ./index.test.js
browser test
✓ opens a page (11ms)
console.log index.test.js:4
Test undefined
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.992s, estimated 1s
Ran all test suites.
Teardown 0.23915022164799082
In my use case inside doSomethingAsync
an instance of puppeteer
browser would be created.
AFAIK there is no other way to do something before all tests and after all tests.
NPX:
LMRC6785:jest-bug dis$ npx envinfo --preset jest
npx: installed 1 in 1.526s
System:
OS: macOS High Sierra 10.13.4
CPU: x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
Binaries:
Node: 9.4.0 - /usr/local/bin/node
Yarn: yarn install v0.24.6
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 5.90s. - ~/.yarn/bin/yarn
npm: 5.6.0 - /usr/local/bin/npm
npmPackages:
jest: 22.4.3 => 22.4.3
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Jest Integration Testing - How to manage global context?
Using the jest one time setup I can execute my setup only once, and store the results as variables in the NodeJS global...
Read more >Setup and Teardown - Jest
Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do...
Read more >Mocha — Global Setup and Teardown (before/after)
Mocha is a powerful testing framework for Node.js. It gives you dozens of helpful utilities to create a convenient testing setup.
Read more >Configuring Jest - API Manual
The bail config option can be used here to have Jest stop running tests after n ... test-runner to only run in serial...
Read more >Documentation - Mocha
Disable this to ensure you can run the test suite multiple times. If disabled, be sure to dispose mocha ... Toggle execution of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is by design - every single test file gets its own context. If you want to access something set up in a
globalSetup
you need to do something like what https://github.com/smooth-code/jest-puppeteer is doing.Importing your
globalSetup
from a file is not supported (and it won’t be).If you have more questions about this topic, we recommend using StackOverflow or our discord channel.
If you think anything can be clarified in the docs, a PR is most welcome!
Tests do not share the same context, each individual test file has their own.
global{Setup,Teardown}
is not part of a single test’s context (otherwise it’d have to run multiple times instead of exactly once), and it’s impossible for it to be.If you want to run setup a single time (typically starting up some long-running service), use
globalSetup
. If you need something to run in context before each test, usesetupFiles