Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow globalSetup to pass variables to test suite and globalTeardown #9037

Closed
alesmenzel opened this issue Oct 10, 2019 · 5 comments
Closed

Comments

@alesmenzel
Copy link
Contributor

🚀 Feature Proposal

Currently, there is no way (not that I know of) to pass any variable from the globalSetup in order to be able to either use it in the tests or to clear it in the globalTeardown.

Motivation

Imagine the following test case:
I am writing acceptance tests for a logger, that can send the logs to a web server through HTTP. In the test suite, I would like to create a simple HTTP server in the globalSetup and be able to use it in the test suite without the need to specify an own environment (jest-environment-node) that runs for every test file, unlike the globalSetup that runs once.
Then after the tests are done, I would like to be able to close the server in globalTeardown, but currently, there is no way to pass a reference to it.

Example

// globalSetup.js
const createHttpServer = require("./servers/http-server");

module.exports = async () => {
 const http = createHttpServer(5000);
 return { http };
};
// test/spec file
describe("sample-test", () => {
  it("send a request", (next) => {
   const logger = new Logger({ method: "POST", url: "http://localhost:5000" })
   // setup could be exposed as global variable
   global.setup.http.on("request", (req, res) => {
     // ... simplified
     expect(body).toBe("xxx")
     next()
    })
    logger.log("xxx")
  })
})
// globalTeardown.js
module.exports = async (setup) => {
  await promisify(setup.http.close)
};

Pitch

Why does this feature belong in the Jest core platform?

Current global handlers use cases are too limited in order to be useful.

@thymikee
Copy link
Collaborator

We won't add this, because globalSetup/Teardown and tests are running in different environments (tests are sandboxed). You'll need to create a custom test environment to inject variables that are e.g. read from the file system: https://jestjs.io/docs/en/configuration#testenvironment-string. See how https://github.com/smooth-code/jest-puppeteer does it

@thymikee
Copy link
Collaborator

However, we could discuss the global teardown part, as they run in the same context. What do you think @SimenB?

@SimenB
Copy link
Member

SimenB commented Oct 10, 2019

Workaround for now is just to assign to a global in setup.

I still think some limited (e.g. json serialized) injection from setup into tests make sense (although that won't cover what OP wants), and we could inject that into teardown as well (not serialized in that case).

@rkuhn
Copy link

rkuhn commented Sep 21, 2020

It should be noted that at least with version 26.4.2 it is possible to compute values in jest.config.js, put them in globalConfig.globals as well as global, and thus have them available both in tests and global setup/teardown functions.

EDIT: This even works when the values are computed asynchronously — just put an object into globalConfig.globals that you also keep a reference to from global and then use globalSetup to fill that object.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants