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

mock-anything

v0.4.0

Published

Tiny utility to temporarily replace a function and restore it

Readme

mock-anything

https://www.npmjs.com/package/mock-anything

Tiny TypeScript utility to temporarily replace a method on an object and restore it.

No test framework.
No module mocking.
No magic.

check example.js for more examples :)

Questions or issues? Open an issue on GitHub – happy to help.


✨ What is this?

mock-anything lets you override a function explicitly and safely:

  • return values (sync)
  • throw errors
  • resolve promises (async)
  • apply behavior once (once)
  • apply behavior multiple times (times)
  • apply behavior based on arguments (withArgs)
  • inspect call count and arguments (called, calledArgs)
  • apply behavior on call index (onCall)
  • reset data but keeps the mock (reset)
  • restore the original function
  • restore all mocks at once (restoreAll)
  • conditional behavior (until) - please see examples to avoid bugs or loops

Perfect for:

  • scripts
  • tools
  • small tests
  • POCs
  • Node utilities

❌ What this is NOT

  • ❌ Not a test framework
  • ❌ Not module/import mocking
  • ❌ Not a Jest/Sinon replacement
  • ❌ No globals, no loaders

You must have access to the object + method reference.


📦 Install

npm install mock-anything

🚀 Basic Usage

import { mock } from "mock-anything";

const api = {
  getUser() {
    return { id: 1 };
  },
};

const m = mock(api, "getUser").returns({ id: 42 });

api.getUser(); // { id: 42 }
api.getUser(); // { id: 42 }

console.log(m.called()); // 2

m.restore();

api.getUser(); // { id: 1 }

🔁 Async example

const api = {
  async fetchUser() {
    return { id: 1 };
  },
};

const m = mock(api, "fetchUser").resolves({ id: 99 });

await api.fetchUser(); // { id: 99 }

m.restore();

🔂 once()

Apply behavior only on the first call.

mock(api, "fetch")
  .once().throws(new Error("timeout"))
  .returns("ok");

api.fetch(); // throws
api.fetch(); // "ok"
api.fetch(); // "ok"

🎯 withArgs()

Apply behavior based on arguments.

mock(api, "calc")
  .withArgs(1, 2).returns(3)
  .withArgs(2, 2).returns(4)
  .returns(0); // fallback

api.calc(1, 2); // 3
api.calc(2, 2); // 4
api.calc(9, 9); // 0

Works with async too:

mock(api, "fetchUser")
  .withArgs(1).resolves({ id: 100 })
  .withArgs(2).resolves({ id: 200 })
  .resolves({ id: -1 });

📊 API

mock(target, key)
  .returns(value)        // return a value
  .throws(error)         // throw an error
  .resolves(value)       // resolve a promise
  .once()                // apply behavior once
  .times(n)              // apply behavior multiple times
  .withArgs(...args)     // apply behavior based on arguments
  .called()              // get call count
  .calledArgs()          // get arguments of each call
  .reset()               // reset data but keeps the mock
  .onCall(n)             // apply behavior based on call index
  .until(() => bool)     // apply behavior based on a boolean condition
  .restore();            // restore the original function

restoreAll();             // restore all mocks at once

🧠 Design principles

  • Explicit > magic
  • Small API surface
  • Safe monkey-patching
  • Easy to reason about
  • Easy to remove

📜 License

MIT