Allow globalSetup to pass variables to test suite and globalTeardown
See original GitHub issue🚀 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5
Top GitHub Comments
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).
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.