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

timeout-monitor

v0.3.0

Published

Monitor setTimeout and setInterval to help detect uncleared timers

Downloads

15

Readme

timeout-monitor

npm version

Monitor setTimeout and setInterval to help detect uncleared timers

Introduction

timeout-monitor is a tool to help detect uncleared timers in your code. It is particularly useful when running tests where uncleared timers can prevent garbage collection, slow tests down, interfere with later tests and even prevent the node test process from completing

Installation

Install using npm:

npm install timeout-monitor

Usage

Import class from package

Firstly we need to import the TimeoutMonitor class from the timeout-monitor package:

const TimeoutMonitor = require('timeout-monitor');

or, using ES6 modules

import TimeoutMonitor from 'timeout-monitor';

Create instance and attach it to global object

Then we need to create an instance of TimeoutMonitor and attach it to the global or window object:

const monitor = new TimeoutMonitor();
monitor.attach(window);

For convenience we can also supply the global object in the constructor

const monitor = new TimeoutMonitor(window);

Attaching timeout-monitor replaces the global setInterval, clearinterval, setTimeout and clearTimeout functions. The replacements will behave the same as the originals so can be used as usual by any other code

When a call is made to setInterval or setTimeout, the monitor will record it and will also record the code location where the call was made. When a timer is cleared or a timeout is triggered, the monitor will remove that reference

Get current uncleared timers

We can call the report() method at any time to get currently open intervals and timeouts:

const report = monitor.report());

Detach from global object

Finally, to detach the monitor and restore the original global timer functions, we call the detach() method:

monitor.detach();

Example

To find (and clear) uncleared timers produced by any test in a test suite, in our global test setup file we can add a beforeEach and afterEach to run before and after every test, eg:

const TimeoutMonitor = require('timeout-monitor');
const monitor = new TimeoutMonitor();

beforeEach(() => {
    monitor.attach(window);
});

afterEach(() => {
    const report = monitor.report();

    report.intervals.forEach([intervalId, location] => {
        const { file, line, char } = location;
        console.warn('Uncleared call to setInterval at "%s", line %d, column %d', file, line, char);
        window.clearInterval(inervalId);
    });

    report.timeouts.forEach([timeoutId, location] => {
        const { file, line, char } = location;
        console.warn('Uncleared call to setTimeout at "%s", line %d, column %d', file, line, char);
        window.clearTimeout(timeoutId);
    });

    monitor.detach();
});

Combining with Jest or Sinon

If combining timeout-monitor with tests using Jest's timer mocks or Sinon's fake timers we will usually want to attach timeout-monitor before the fake timers and detach after the fake timers have been restored. timeout-monitor won't be able to detect calls to fake timers but uncleared fake timers should not interfere with subsequent tests anyway, so we want timeout-monitor to only look for timers that are set outside the fake implementation