question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global state is accessible when running with runInBand

See original GitHub issue

How 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ojongeriuscommented, Apr 25, 2018

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.

▶ yarn jest
yarn run v1.5.1
$ /Users/ojongerius/repos/jest-bug/node_modules/.bin/jest

 RUNS  ./mongo-insert.test.js
 FAIL  ./mongo-aggregate.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'getConnectionString' of undefined

       9 |     console.log('Setup MongoDB Test Environment');
      10 |
    > 11 |     this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
      12 |     this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
      13 |
      14 |     await super.setup();

      at MongoEnvironment.setup (mongo-environment.js:11:57)

 FAIL  ./mongo-insert.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'getConnectionString' of undefined

       9 |     console.log('Setup MongoDB Test Environment');
      10 |
    > 11 |     this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
      12 |     this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
      13 |
      14 |     await super.setup();

      at MongoEnvironment.setup (mongo-environment.js:11:57)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        0.454s
Ran all test suites.
Teardown mongod
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c /Users/ojongerius/repos/jest-bug/node_modules/.bin/jest
Directory: /Users/ojongerius/repos/jest-bug
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/Users/ojongerius/repos/jest-bug/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

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 and yarn test. Pull down https://github.com/ojongerius/jest-bug, running yarn test will fail and yarn test-inband will succeed.

What is the expected behavior? Running more n+1 tests will succeed

Please provide your exact Jest configuration

module.exports = {
  globalSetup: './setup.js',
  globalTeardown: './teardown.js',
  testEnvironment: './mongo-environment.js'
};

Run npx envinfo --preset jest in your project directory and paste the results here

▶ npx envinfo --preset jest
npx: installed 1 in 3.967s

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
  Binaries:
    Node: 8.10.0 - ~/.nvm/versions/node/v8.10.0/bin/node
    Yarn: 1.5.1 - /usr/local/bin/yarn
    npm: 5.8.0 - ~/.nvm/versions/node/v8.10.0/bin/npm
  npmPackages:
    jest: ^22.4.3 => 22.4.3
1reaction
dbartholomaecommented, Oct 16, 2018

Thanks! For those looking, I set up a feature request at #7184.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest global setup leads to inconsistent test pass/failure
The issue was caused by multiple tests running at the same time, adding the --runInBand flag (which forces test to be run serially)...
Read more >
Jest CLI Options
You can run jest --help to view all available options. Many of the options shown ... This implies --runInBand , making tests run...
Read more >
Jest Configuration And Debugging Jest Based Tests
#2) Globals: This config is used to set global variables that should be available with each test. "jest" { "globals": { "globalVar": "test ......
Read more >
Jest global variables that persist - iTecNote
CLI options are documented and also accessible by running the command jest --help . You'll see the option you are looking for :...
Read more >
Angular 14 + Jest: tests running fails with "Unknown arguments
Open on of the unit tests and try to run it through WebStorm. What is the expected result? ... Check the Available in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found