npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

global-leaks-finder

v1.0.1

Published

Manage your tests side-effects

Downloads

5

Readme

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.