12

I have a global setup in Jest.

"jest": {
    "globalSetup": "./setup.js"
  }

Within the setup, I have an async function which writes a session key to global

const axios = require('axios');

async function getSessionKey() {
    const response = await axios.post("http://apiUrl", {
        username: "K",
        password: "passw0rd"
    });
    sessionKey = response.data.sessionKey;
    global.sessionKey = sessionKey;
}

module.exports = getSessionKey;

Whatever session key I am saving in global is not available in the test file. global.sessionKey is undefined in the below test.

test("create session", () => {
    expect(global.sessionKey).toBeTruthy();
});

I want some way to set the sessionKey in a global object from setup file. The session key should be available before I run any of my tests.

user3335966
  • 2,673
  • 4
  • 30
  • 33
vijayst
  • 20,359
  • 18
  • 69
  • 113
  • Possibly a bug with Jest - https://github.com/facebook/jest/issues/5424. For now, I replaced Jest with Mocha in my project and everything is good! – vijayst Jan 30 '18 at 17:05

3 Answers3

6

globalSetup/globalTeardown can't be used to inject context/global variables to sandboxed test suites/files. Use setupFiles/setupFilesAfterEnv instead.

Other way you can customize the test runtime through testEnvironment, more details see here: Async setup of environment with Jest

cdoublev
  • 709
  • 6
  • 18
Allen
  • 4,431
  • 2
  • 27
  • 39
  • 2
    how should I use the `setupFiles`? it says in the docs that I can add this as a field in package.json in jest subfields, but it only says it should be an array and that the default value is `[ ]`. so what should I do if I want to add for instance, a `jest.setTimeout(10000);` config there? – ganjim Sep 10 '18 at 13:06
3

I spend a lot of time figuring this out right. The only fully working solution was using testenvironments. This supports async functions as well as globals properly. Each test suite uses a separate instance of the testenvironment.

const NodeEnvironment = require('jest-environment-node');

class TestEnvironment extends NodeEnvironment {
  constructor(config) {
    super(config);
  }

  async setup() {
    await super.setup();
    this.global.db = await asyncCode();
  }

  async teardown() {
    this.global.db = null;
    await asyncCode2();
    await super.teardown();
  }

  runScript(script) {
    return super.runScript(script);
  }
}

module.exports = TestEnvironment;

I added a reference to this file in the Jest Config: "testEnvironment": "./testEnvironment" for the file testEnvironment.js

I'm not sure though why there are so many different config options like setupFiles, globals, testEnvironment, ...

user3335966
  • 2,673
  • 4
  • 30
  • 33
Florian Richter
  • 131
  • 2
  • 8
  • 1
    This runs per test suite and can't share memory between instances. It's basically like beforeAll and afterAll hooks. – José Cabo Dec 14 '19 at 17:30
3

Setting an env variable like process.env.SESSION_KEY = sessionKey from globalSetup seems to work for me in subsequent tests.

I can access the value properly from any test. Not sure if this is a bug or a feature.

andreialecu
  • 3,639
  • 3
  • 28
  • 36
  • It’s between a bug and a feature. I use it since it’s pretty much the only meaningful way to communicate between Jest setup and tests. There’s supposed to be a proper feature for passing serializable data from setup to tests, and once that is out, the env way may be closed. – akauppi Jul 16 '21 at 18:23