New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

global-leaks-finder

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

global-leaks-finder

Manage your tests side-effects

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Global Leaks finder

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.

Why?

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.

Install

Does not require any additional dependencies, just itself.

npm install global-leaks-finder

Usage

mocha --check-leaks node_modules/global-leaks-finder/index.js <your test files here>

Mocha's check-leaks options

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.

How does it work

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.

Issues

Please feel free to contact or email me if there are any issues.

Keywords

FAQs

Package last updated on 02 Mar 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc