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 🙏

© 2026 – Pkg Stats / Ryan Hefner

eslint-plugin-jest-when

v0.0.9

Published

ESLint plugin for enforcing best practices with jest-when. Made with Claude AI for personal use.

Readme

ESLint Plugin for Jest-When Best Practices

This ESLint plugin provides rules to help you use the jest-when library effectively and follow recommended patterns.

be careful. This is not best example of code generated by AI on request of someone inexperinced in JS/TS dev.

Installation

You'll first need to install ESLint and jest-when:

npm install eslint jest-when --save-dev

Next, install the plugin:

npm install eslint-plugin-jest-when --save-dev # Replace with the actual package name

Usage

Add eslint-plugin-jest-when to the plugins section of your .eslintrc configuration file. You can then configure the rules you want to use under the rules section.

{
    "plugins": [
        "eslint-plugin-jest-when"
    ],
    "rules": {
        "your-plugin-name/no-reset-when-mocks-at-end": "warn",
        "your-plugin-name/no-reset-when-mocks-in-after-each": "warn",
        "your-plugin-name/prefer-jest-when-with-debug": "off", // Or "warn" to enable with debug suggestion
        "your-plugin-name/prefer-jest-when": "warn", // Use this OR prefer-jest-when-with-debug
        "your-plugin-name/prefer-once-mock-methods": "warn",
        "your-plugin-name/verify-when-mocks-in-after-each": "error"
    }
}

Note: You should typically enable either prefer-jest-when or prefer-jest-when-with-debug, but not both. prefer-jest-when-with-debug includes an extra debug logging suggestion when applying the fix.

Supported Rules

no-reset-when-mocks-at-end

Discourages using resetAllWhenMocks() at the end of it or test blocks. It is recommended to use verifyAllWhenMocksCalled() in an afterEach block instead for ensuring all mocks were called and resetAllWhenMocks() at the beginning of a test case if needed.

Incorrect:

it('should do something', () => {
  // ... test logic ...
  resetAllWhenMocks();
});

Recommended:

afterEach(() => {
  verifyAllWhenMocksCalled();
});

it('should do something', () => {
  resetAllWhenMocks(); // If needed at the start
  // ... test logic ...
  // No resetAllWhenMocks here
});

no-reset-when-mocks-in-after-each

Discourages using resetAllWhenMocks() within an afterEach block. resetAllWhenMocks() should ideally be used at the beginning of individual test cases if necessary, while verifyAllWhenMocksCalled() is recommended for afterEach.

Incorrect:

afterEach(() => {
  resetAllWhenMocks();
  verifyAllWhenMocksCalled();
});

Recommended:

afterEach(() => {
  verifyAllWhenMocksCalled();
});

it('should do something', () => {
  resetAllWhenMocks(); // Use at the start of the test if required
  // ...
});

prefer-jest-when

Suggests using the jest-when library for mocking methods like mockReturnValue, mockResolvedValue, etc.

Incorrect:

myMock.mockReturnValue(1);

Recommended:

import { when } from 'jest-when';

when(myMock).calledWith(...).mockReturnValue(1); // Add specific arguments to calledWith

prefer-jest-when-with-debug

Similar to prefer-jest-when, this rule also suggests using jest-when but includes a debug helper (when.allArgs) in the suggested fix to make it easier to inspect the arguments a mock was called with. Disable prefer-jest-when if using this rule.

Incorrect:

myMock.mockReturnValue(1);

Recommended (with fix enabled):

import { when } from 'jest-when';

when(myMock).calledWith(when.allArgs((args) => {
  console.log(args); // <-- set breakpoint here to debug sent arguments
  return true;
})).mockReturnValue(1);

prefer-once-mock-methods

Encourages the use of *Once variants (e.g., mockReturnValueOnce) for mock methods when using jest-when, promoting better test isolation.

Incorrect:

import { when } from 'jest-when';

when(myMock).calledWith(1).mockReturnValue(1);

Recommended:

import { when } from 'jest-when';

when(myMock).calledWith(1).mockReturnValueOnce(1);

verify-when-mocks-in-after-each

Ensures that verifyAllWhenMocksCalled() is included in an afterEach block when jest-when is used in the test file. This helps confirm that all configured when mocks were called during the test.

Incorrect (when using jest-when but no verification):

import { when } from 'jest-when';

it('should use a mock', () => {
  when(someMock).calledWith(1).mockReturnValue(true);
  // ...
});

Recommended:

import { when, verifyAllWhenMocksCalled } from 'jest-when';

afterEach(() => {
  verifyAllWhenMocksCalled();
});

it('should use a mock', () => {
  when(someMock).calledWith(1).mockReturnValue(true);
  // ...
});

License

MIT