Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't get the global variable which was set in globalSetup in test code?

Tags:

jestjs

I use Jest to do unit test in node.
And I use the new feature globalSetup which come in Jest v22.
I have defined a global variable in globalSetup.
But I can't get it in the test code. Console log is undefined.
Anyone in this question?
Thanks.

Jest version: 22.0.0
node version: 8.9.0
yarn version: 1.3.2
OS: mac High Sierra 10.13.2

The code as follow:

// package.json
{
  "jest": {
    "globalSetup": "<rootDir>/src/globalSetupTest.js"
  }
}

// globalSetupTest.js
module.exports = async function() {
  global.foo = 'foo';
  console.log(`global setup: ${global.foo}`);
};

// App.test.js
describe('APP test', () => {
  it('renders without crashing', () => {
    console.log({ foo: global.foo });
  });
});

// test result
yarn run v1.3.2
$ node scripts/test.js --env=node --colors
global setup: foo
 PASS  src/App.test.js
  APP test
    ✓ renders without crashing (5ms)

  console.log src/App.test.js:3
    { foo: undefined }

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.354s, estimated 1s
Ran all test suites.
like image 302
zzm Avatar asked Jan 11 '18 13:01

zzm


People also ask

Why are there no global variables?

Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use. A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code.

How do you declare a global variable in main?

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

Are variables global by default?

Global variables are extern by default - that means they can be used by other translation unit (can simply be other source files). That said global variables are not automatically usable from other source files unless there are corresponding extern declaration on those source files.


1 Answers

There's a solution offered from Jest people themselves: https://jestjs.io/docs/en/puppeteer.html. Note that if you're using CRA, this won't work out of the box (solution below), cause it currently doesn't support testEnvironment option for Jest.

Anyways:

  • In Jest config file you setup paths to global setup, teardown and test environment scripts
  • In global setup script, you create a browser and write it's WSEndpoint to a file
  • In global teardown script, you simply close the browser
  • In testEnvironment script, you read WSEndpoint from the file you saved before and then use it to connect to the running browser - after this, browser is available in your tests by using a global variable

If you're using CRA, you can use a custom setup for these tests and run them completely separately. And if you're using Puppeteer for e2e tests, this is probably what you want to do anyway.

You just add another script to your package.json: "test:e2e": "jest -c ./jest-e2e.config.js" and set it up as you want. Now you will have npm run test for unit tests and npm run test:e2e for end to end tests.

like image 116
Milan Krstic Avatar answered Sep 20 '22 14:09

Milan Krstic