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

to-mock

v2.0.1

Published

The utility to mock class and objects. You can use mocked classes and objects for your unit and integration tests

Readme

to-mock

NPM package version

The to-mock module helps you create mocked classes and objects. Your tests stay up to date with your defined classes automatically — no need to maintain separate mocks or interfaces. This lets you write unit tests against a contract rather than an implementation detail, keeping your tests isolated and easy to maintain.

Works with any test framework: jest, vitest, node:test, tape, jasmine, mocha and others. Includes TypeScript type declarations out of the box.

You can mock Date, RegExp and other native objects.

Installation

npm i to-mock --save-dev

Examples

  1. Jasmine - example with jasmine and toMockedInstance
  2. Jest/Typescript - example with jest and typescript
  3. Best practice - example with setting to-mock module for jest framework

API

  1. toMock
  2. toMockedInstance
  3. setGlobalKeepUnmock
  4. setGlobalMockMethod
  5. objectKeepUnmock
  6. functionKeepUnmock

toMock

The library ships as both ESM (dist/index.mjs) and CommonJS (dist/index.cjs) with bundled TypeScript declarations (dist/index.d.ts).

// MyArray.js
export default class MyArray {
  constructor(array) {
    this.array = array.slice();
  }

  clone() {
    return this.array.slice();
  }
}

// MyArray.test.js
import MyArray from './MyArray.js';
import { toMock } from 'to-mock';

describe('Your spec', () => {
  // MyArray is not modified — a new mocked subclass is returned
  const MockedMyArray = toMock(MyArray);
  const mockedInstance = new MockedMyArray();

  it('is instance of MyArray', () => {
    expect(mockedInstance instanceof MyArray).toBeTruthy();
  });

  it('method does not throw', () => {
    expect(() => mockedInstance.clone()).not.toThrow();
  });
});

Mock native object

// MyDate.test.js
import { toMock } from 'to-mock';

describe('Your spec', () => {
  const MockedDate = toMock(Date);
  const RealDate = Date;

  beforeEach(() => {
    Date = MockedDate;
  });

  afterEach(() => {
    Date = RealDate;
  });

  it('you can mock date', () => {
    jest.spyOn(MockedDate, 'now').mockReturnValue(1);

    expect(Date.now()).toEqual(1);
  });
});

toMockedInstance

Creates a mocked instance directly, with optional property overrides applied on top. Equivalent to calling toMock, constructing an instance, and applying Object.assign — but in one step.

import { toMockedInstance } from 'to-mock';

describe('Your spec', () => {
  it('creates a mocked Date instance with a default getTime', () => {
    const dateWithDefault = toMockedInstance(Date, { getTime: () => 1 });

    expect(dateWithDefault.getTime()).toEqual(1);
  });
});

setGlobalKeepUnmock

Sets a global predicate applied to every toMock / toMockedInstance call that does not supply its own keepUnmock argument. Return true from the predicate to leave a property unmocked.

In v2, objectKeepUnmock is active by default — you only need to call this to override or clear the global predicate.

Two built-in predicates are provided: objectKeepUnmock and functionKeepUnmock.

import { toMockedInstance, setGlobalKeepUnmock, objectKeepUnmock } from 'to-mock';

// Preserve Object.prototype methods (toString, hasOwnProperty, …) globally
setGlobalKeepUnmock(objectKeepUnmock);

// Per-call keepUnmock — keep getDay unmocked on this instance only
function keepGetDay({ property }) {
  return property === 'getDay';
}

const dateWithDefault = toMockedInstance(
  Date,
  { getTime: () => 1 },
  keepGetDay,
);

const dayFromMock = Reflect.apply(dateWithDefault.getDay, new Date(), []);
const dayFromDate = new Date().getDay();

expect(dateWithDefault.getTime()).toEqual(1);
expect(dayFromMock === dayFromDate).toEqual(true);

setGlobalMockMethod

Overrides the factory used to produce mock functions. The factory is called once per mocked method and must return a function. Defaults to () => function mockMethod() {}.

Use this to integrate with your testing framework's spy/mock system:

import { toMockedInstance, setGlobalMockMethod } from 'to-mock';

setGlobalMockMethod(jest.fn);

test('tracks calls', () => {
  const date = toMockedInstance(Date);
  date.getTime();

  // All jest.fn() methods are available
  expect(date.getTime).toHaveBeenCalled();
});

objectKeepUnmock

Built-in predicate that preserves all properties inherited from Object.prototype (e.g. toString, hasOwnProperty). This is the default global predicate in v2 — it is active without any setup.

Pass it as the keepUnmock argument for per-call use, or call setGlobalKeepUnmock(objectKeepUnmock) to restore it after clearing.

import { toMock, objectKeepUnmock } from 'to-mock';

// per-call:
const Mocked = toMock(MyClass, objectKeepUnmock);

functionKeepUnmock

Built-in predicate that preserves all properties inherited from Function.prototype (e.g. call, apply, bind). Useful when you need the constructor chain to remain functional.

import { toMock, functionKeepUnmock } from 'to-mock';

const Mocked = toMock(MyClass, functionKeepUnmock);

TypeScript

The package ships with TypeScript declarations — no additional @types package needed.

import { setGlobalMockMethod, toMockedInstance } from 'to-mock';
import * as utils from '../utils.js';

jest.mock('../utils', () => {
  const original = jest.requireActual('../utils');

  setGlobalMockMethod(jest.fn);

  return toMockedInstance(
    original,
    { __original__: original },
    ({ property }) => property === 'once',
  );
});

Best practice

Recommended setup file for jest (configured via setupFiles). The same pattern works for vitest with its setupFiles option. For node:test, call the setup file manually before running tests.

// setupJest.js
import { setGlobalMockMethod } from 'to-mock';

// Replace every mocked method with jest.fn()
setGlobalMockMethod(jest.fn);
// *Spec.js
import { toMockedInstance } from 'to-mock';

// Use toMockedInstance everywhere — no need to call new

Migration to v2

Named exports only — default export removed

v1 exported toMock as the default export. v2 removes the default export; use the named export instead.

// v1
import toMock from 'to-mock';

// v2
import { toMock } from 'to-mock';

objectKeepUnmock is now the default global predicate

In v1, Object.prototype methods (e.g. toString, hasOwnProperty) were mocked by default. In v2, objectKeepUnmock is applied globally out of the box — Object.prototype methods are preserved without any setup.

If you need the v1 behaviour (mock everything including Object.prototype), call:

import { setGlobalKeepUnmock } from 'to-mock';

setGlobalKeepUnmock(null);

Contributing

Contributing to this repository is done via Pull-Requests. Any commit must follow these rules (validated automatically on commit):

  1. type (build, ci, chore, docs, feat, fix, perf, refactor, revert, style, test)
  2. scope in brackets — one-word description of the changed area
  3. colon :
  4. message (lower-case)

Examples: fix(mock): handle null prototype, feat(types): add keepUnmock overload

Use npm run commit for an interactive prompt that validates and formats the message automatically. See commitizen/cz-cli for details.