Allow to share global state between tests from globalSetup
## 🚀 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](https://github.com/nock/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 framewo
Posted by dbartholomae on Oct 16, 2018, 2:58 PM
Tags
Related issues
Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments
onboarding (Software)
Minecraft Dungeons II (1912410)
We are seeing the realtime result of MS buying Github and buying influence.
macOS 15 Sequoia clobbers _nixbld1-4 users
News + Polls
Implement Pylint
Add Zig
Unable to run sandboxed process when run as elevated under applocker
[Feature request] Show browser error overlay for runtime errors
Allow to share global state between tests from globalSetup
## 🚀 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](https://github.com/nock/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 framewo
Comments (0)
No comments yet. Start the discussion.