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 to share global state between tests from globalSetup

See original GitHub issue

šŸš€ Feature Proposal

Add a global property to the this of globalSetup and globalTeardown async functions that can be used to set global variables that can be accessed in all tests via global. The same global is shared across all tests.

Motivation

While jest was in the beginning used only for frontend testing, it has moved in the direction of becoming a general test framework. Especially for backend integration tests there is a tradeoff between test speed and departmentalization: Starting up a new backend instance for each individual test usually isnā€™t feasible. Therefore most other test frameworks like mocha or jasmine provide possibilities to share state between tests, e. g. the backend instance. Usage examples include mocking http requests via nock.

Example

Letā€™s assume an integration test that tests backend and database integration. The setup could look like this:

const backend = require('backend')

async function setupApp () {
  await new Promise((resolve, reject) => {
    backend.start().then((instance) => {
      this.global.backend = instance
    })
  })
}

module.exports = setupApp

And using the global could be done like this:

const request = require('supertest')
test('should call out to that fancy other api', () => {
  request(jest.globals.backend.url)
    .post('/some-endpoint')
    expect(200)
})

Pitch

As far as I know this change currently cannot be implemented outside of the main jest framework. Closest is an environment, but environments are sandboxed and do not share global state.

Open questions

How to best implement it?

I donā€™t know the jest code well enough to have an idea how to best implement this. It might e. g. be easier to make the global available via global, or even jest.getGlobals().

Can we prevent misuse?

Sharing state between tests can lead to sideffects and random test breakage. One possible solution would be to make the jest.globals read-only, but I am not sure whether this is feasible without massively reducing which kind of objects can be stored.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:179
  • Comments:38 (3 by maintainers)

github_iconTop GitHub Comments

48reactions
DanLambecommented, Nov 26, 2018

I agree with @dbartholomae on this issue, I find it hard to recommend jest for all types of testing without the ability to share state between tests. I have a real usecase currently where the company I work for wanted to standardize our testing frameworks so I do to start using Jest over Mocha for my functional API testing for our react app. that was a mistake given that I have to fetch a new bearer token for every test file with no way of retaining that token to a variable ā€œgloballyā€.

38reactions
adrianmclicommented, Apr 19, 2020

I think my desired use-case is probably not possible, but I figured Iā€™d chime in here as well.

I need to run integration tests with a server that is instantiated in-memory. That means Iā€™m doing something like this:

const server = await startServer()

And then I need to use that server instance over a bunch of my tests files. To make matters worse, that particular startServer function takes about 5 to 10 seconds to startup (this is non-negotiable).

If instances cannot be passed around, then it sounds like Iā€™m out of luck. But I would really love it if someone could tell me that I am wrong and that a solution is just around the corner.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest global variables that persist - Stack Overflow
Test cases should be able to run independently from one another. Allowing some mutation to a global variable to be persisted across all...
Read more >
Globals - Jest
This is often useful if you want to clean up some global setup state that is shared across tests. For example:.
Read more >
Jest Globals - w3resource
This is mostly useful if you want to clean up some global setup state that is shared across tests. For instance:
Read more >
Configuring Jest - API Manual
Automatically clear mock calls and instances between every test. ... This option allows the use of a custom global setup module which exports...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mappingĀ ......
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