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.

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
SimenBcommented, 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).

0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't get the global variable which was set in globalSetup ...
In Jest config file you setup paths to global setup, teardown and test environment scripts; In global setup script, you create a browser...
Read more >
Configuring Jest - API Manual
Note: Any global variables that are defined through globalSetup can only be read in globalTeardown . You cannot retrieve globals defined here in...
Read more >
Jest Global Setup - Part 10 - YouTube
Learn how to create a global setup when testing with Jest.
Read more >
Configuring Jest
A set of global variables that need to be available in all test environments ... This option allows the use of a custom...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.
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