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:
- Created 5 years ago
- Reactions:179
- Comments:38 (3 by maintainers)
Top GitHub Comments
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ā.
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:
And then I need to use that
server
instance over a bunch of my tests files. To make matters worse, that particularstartServer
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.