https://github.com/noomorph/jest-global-leakage
Demonstrates an issue with globalThis leakage affected by import order
https://github.com/noomorph/jest-global-leakage
Last synced: 12 months ago
JSON representation
Demonstrates an issue with globalThis leakage affected by import order
- Host: GitHub
- URL: https://github.com/noomorph/jest-global-leakage
- Owner: noomorph
- License: mit
- Created: 2023-04-19T10:33:33.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T16:34:36.000Z (about 3 years ago)
- Last Synced: 2025-03-17T10:53:29.070Z (over 1 year ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jest Global Leakage Bug
This repository demonstrates the leakage of global variables coming from the unsandboxed `globalThis` into the sandboxed one, held by the test environment.
## Description
To reproduce this issue, we need:
* a custom environment class, e.g. `custom-environment.js`:
```js title="custom-environment.js"
globalThis.__LEAKAGE__ = true;
module.exports = require('jest-environment-node');
```
* and a test file, e.g. `leakage.test.js`:
```js title="leakage.test.js"
it('should not leak', () => {
expect(globalThis.__LEAKAGE__).toBeUndefined();
});
```
For better or for worse, but such test would fail :red_circle:, due to [this commit to Jest 28.x](https://github.com/facebook/jest/commit/5247e1ff6bb13a88f95597979d938fd74c33b655).
However, if you swap the lines, it will pass:
```js
module.exports = require('jest-environment-node');
globalThis.__LEAKAGE__ = true;
```
## Question
Is this the intended behavior? Or global variables introduced via `testEnvironment` module (or its dependencies) should not be copied to the sandboxed `this.global`?
## Steps to reproduce
1. Clone this repository.
2. Install the dependencies:
```bash
npm install
```
3. Run the test with the initial import order (the test should fail):
```bash
npm test
```
## Confirmed versions
* Jest version: 28.0.0 - 29.5.0
* Node version: 14.x - 18.x
## License
This project is licensed under the **MIT License**.