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

mockable-modules

v1.0.0

Published

Easy to use mocking for ES Modules

Downloads

63

Readme

mockable-modules

A non-transpiling, non-configuration intense way to mock ES Modules for your tests. Built specifically to work nicely with the native Node.js test runner.

Install

npm install mockable-modules

Usage

Some code to test:

import { unpredictableFunction } from "db";

export const unpredictable = mockable(unpredictableFunction);

export async function action() {
	const result = unpredictable();

	return result;
}

And a test:

import { assert } from "mockable-modules/assert";
import { action, unpredictable } from "./index.js";

test("synchronous passing test", (t) => {
	const mock = unpredictable.tOverride(t, () => {
		return 12;
	});

	assert.calledOnce(mock);
});

Core API

mockable(fn)

Wraps a function's implementation to make it mockable

Example:

const mockableThing = mockable(thing);

disableMockable()

Disables the library in the given environment. You'll want to call this as early as possible in your production environment.

Example:

disableMockable();

.override(t, fn)

Override a mockable within test context t. Returns fn wrapped as a mock.fn. Clears the mock automatically when the test context ends.

Example:

import { whatever } from "./target.js";

test("a test", (t) => {
  const mockFn = whatever.override(t, () => "yes");

  // do some things

  // assert some things
});

Assertions

All of the assertions provided by this package are for asserting against Node's mock.fn instances.

Mockable Modules includes two ways for importing assertions.

  • import assert from "mockable-modules" - Wraps node:assert/strict to include the assertions from Mockable Modules.
  • import * as assertions from "mockable-modules/assertions" - Import just the Mockable Modules assertions.

assert.notCalled(mockFn)

Assert that mockFn has not been called.

Example:

test("a test", (t) => {
  const mockFn = whatever.override(t, () => "yes");

  // do some things

  assert.notCalled(mockFn);
});

assert.calledOnce(mockFn)

Assert that mockFn has been called exactly once.

Example:

test("a test", (t) => {
  const mockFn = whatever.override(t, () => "yes");

  // do some things

  assert.calledOnce(mockFn);
});

assert.calledOnce(mockFn)

Assert that mockFn has been called exactly once.

Example:

test("a test", (t) => {
  const mockFn = whatever.override(t, () => "yes");

  // do some things

  assert.calledOnce(mockFn);
});

assert.calledOnce(mockFn)

Assert that mockFn has been called exactly once.

Example:

test("a test", (t) => {
  const mockFn = whatever.override(t, () => "yes");

  // do some things

  assert.calledOnce(mockFn);
});

assert.calledTimes(mockFn, count)

Assert that mockFn has been called exactly count times.

Example:

test("a test", (t) => {
  const mockFn = whatever.override(t, () => "yes");

  // do some things

  assert.calledTimes(mockFn, 3);
});

assert.calledWith(mockFn, ...args)

Assert that mockFn has been called at least once with the given args

Example:

test("a test", (t) => {
  const mockFn = whatever.override(t, (x, y) => x + y);

  // do some things

  assert.calledWith(mockFn, 5, 3);
});

License

This code is licensed under the MIT license. See LICENSE for more information.