![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
global-leaks-finder
Advanced tools
A mocha-based testing tool to help find tests which are leaking globals.
The aim is to help catch and avoid tests with unexpected side-effects.
It does add significant time to the job so best run only occasionally. See below for explanation how it works.
Take this example. Before each test the browser
is set and after it is reset. However we do not know what the value was before this unit test ran or if false
is a reliable value for later tests. This is an example of a global leakage and is easily missed.
beforeEach(() => {
process.browser = true;
});
afterEach(() => {
process.browser = false; // BAD
});
Running the tool on this test would throw an error to the console Error: Global has changed
on the test spec which has leaked, allowing you to catch and manage the problem.
The test has now been improved.
let cached;
beforeEach(() => {
cached = process.browser;
process.browser = true;
});
afterEach(() => {
process.browser = cached; // BETTER
});
Ideally its best to use something like Sinon.js Sandboxing, but it is not always possible.
Does not require any additional dependencies, just itself.
npm install global-leaks-finder
mocha --check-leaks node_modules/global-leaks-finder/index.js <your test files here>
I recommend running in conjunction with Mocha's check-leaks
flag. It compares against a small list of non-enumerable globals (i.e. global.newVariable = 'some-value';
would be caught) but would not catch something like process.execPath = 'GLOBAL LEAK';
which this tool would catch.
It runs a global beforeEach
and afterEach
.
For each test it hashes the globals at the start, hashes the globals at the end, and compares the 2 failing if the hash has changed. Hence why it has a hefty performance hit, but hopefully does its job.
Please feel free to contact or email me if there are any issues.
FAQs
Manage your tests side-effects
The npm package global-leaks-finder receives a total of 0 weekly downloads. As such, global-leaks-finder popularity was classified as not popular.
We found that global-leaks-finder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.