Global state is accessible when running with runInBand
See original GitHub issueHow can I get access to the variables I set in globalSetup if tests run in parallel?
I have an issue where the global object I set in globalSetup is not available if there are multiple test suites ran in parallel (this is by default). Running a single suite test or if I set --runInBand to run tests serially, the object is available. I tried to store the global object on node ‘process’ object and I also tried the version where I am using a custom TestEnvironment but I had no luck:
Custom test environment to pass global variable to test suites, is getting a global variable set on globalSetup.
const PuppeteerJsdomEnvironment = require('jest-puppe-shots/lib/jsdom-environment');
class JestPuppeShotsEnv extends PuppeteerJsdomEnvironment {
async setup(config) {
await super.setup(config);
const { allThemesCss } = global;
// make the allThemesCss object available in test suites
Object.assign(this.global, {
allThemesCss
});
}
}
module.exports = JestPuppeShotsEnv;
Allows the use of a custom global setup module which exports an async function that is triggered once before all test suites.
const jestPuppeEnvGlobalSetup = require('jest-puppe-shots/lib/global-setup');
const ScreenShotTestUtils = require('./screenShotUtils');
module.exports = async function globalSetup() {
await jestPuppeEnvGlobalSetup();
const allThemesCss = ScreenShotTestUtils.getAllThemesCss();
global.allThemesCss = allThemesCss;
// process.testSetup = { allThemesCss };
};
There is no proper documentation about this new feature.
jest: v22.4.2 yarn: 1.3.2 OS: Ubuntu 14 node: v8.9.4
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:19 (3 by maintainers)
Top GitHub Comments
Do you want to request a feature or report a bug? A bug.
What is the current behavior? If there are more than 1 tests, running Jest with
--runInBand
will succeed, but without will fail.If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can
yarn install
andyarn test
. Pull down https://github.com/ojongerius/jest-bug, runningyarn test
will fail andyarn test-inband
will succeed.What is the expected behavior? Running more n+1 tests will succeed
Please provide your exact Jest configuration
Run
npx envinfo --preset jest
in your project directory and paste the results hereThanks! For those looking, I set up a feature request at #7184.