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

prostub

v1.3.0

Published

A TypeScript library for creating mocks.

Downloads

131

Readme

ProStub

Description

A mocking/stubbing library for JavaScript, inspired by Mockito. It is evironment-agnostic. The production code at run-time only relies on the JavaScript standard library and tslib. The test code relies on Node.js for running the tests, but the library itself can be used in any JavaScript environment (Node.js, browsers, Deno, etc.).

Features

  • Create mock and spy objects for classes and interfaces
  • Stub methods and properties with various behaviors
  • Verify interactions with mock objects
  • Type-safe API with TypeScript support
  • Lightweight and easy to use
  • Low bundle size (~2.4KB minified + gzipped)
  • Small API surface (only 15 API symbols)

It consists of 3 separate DSLs:

The Mocking DSL

The mock() and spy() functions create mock objects that can be used in place of real objects during testing. Mocks record interactions and can be verified later. By default, all interactions throw errors unless stubbed.

They are created as follows:

import { mock, spy } from "prostub";

class Example {
}

const mockedExample = mock(Example);

const realExample = new Example();
const spiedExample = spy(realExample);

The Stubbing DSL

The stub() function allows you to define custom behavior for methods and properties on mock objects. You can specify return values, thrown errors, or custom implementations for methods, as well as get/set behavior for properties.

A stub always follows the logic stub(<mock>).<method/property> = <behavior>, for example:

import { fixedValue, mock, noop, stub } from "prostub";

class Example {
    method(): number {
        this.property = "changed";
    }
    property: string = "initial";
}

const mockedExample = mock(Example);

// Stubbing a method to return a fixed value
stub(mockedExample).method = noop();
stub(mockedExample).property = fixedValue("stubbed value");

// mockedExample.method() now no longer changes the property
// instead, it does nothing and returns undefined
// mockedExample.property now returns "stubbed value"

Note: The stub() function only works with mock and spy objects created by mock() and spy(). Attempting to use it on real objects will result in a runtime error.

Note 2: stubbed behavior only affects interactions with the mock/spy object itself. This is especially important for spies, as calls to methods on the real object from within other methods will not be affected by stubbing.

Function behaviors

  • answer(fn: (this: OriginalObjectType, ...args: OriginalFunctionArgs) => ReturnType): Defines a custom implementation for a method.
  • callThrough(): Calls the original method implementation on a spy. Throws an error if used on a pure mock.
  • delegateTo(fn: (this: OriginalObjectType, ...args: OriginalFunctionArgs) => ReturnType): Delegates the method call to the provided function.
  • noop(): A no-operation function that does nothing and returns undefined.
  • returnFixed(value: any): Returns a fixed value when the method is called.
  • returnSerial(...values: any[]): Returns a series of values on successive calls.
  • throwOnCall(errorFactory: () => Error): Throws the specified error when the method is called.
  • when(condition: (args: OriginalFunctionArgs) => boolean): Specifies a condition for when the stubbed behavior should apply.

Property behaviors

  • fixedValue(value: any): Always returns the specified fixed value when the property is accessed.
  • defaultValue(value: any): Returns the specified default value when the property is accessed, but allows it to be changed.
  • trackValue(): Tracks the value of the property, allowing it to be read and written like a normal property.

The Verification DSL

The verify() function allows you to assert that certain interactions occurred on mock objects. You can verify method calls, property accesses, and the number of times they were called.

For example:

import { mock, returnFixed, verify } from "prostub";
class Example {
    method(): string {
        return this.property;
    }
    property: string = "initial";
}

const mockedExample = mock(Example);

stub(mockedExample).method = returnFixed("stubbed result");

mockedExample.method(); // Returns "stubbed result"

verify(mockedExample).method.firstInvocation().returned(it =>
    it === "stubbed result"
); // Passes
verify(mockedExample).property.wasNotRead(); // Passes

Building

The library is automatically built when running pnpm install, but it can be re-built manually by running npm run prepare.

Testing

The library is fully tested using Node.js. Tests are executed as part of the building process, run inside the prepare hook, so when install the dependencies:

pnpm install

The tests will be run automatically. To run the tests manually, simply run:

npm test

License

This project is licensed under the MIT License. See the LICENSE file for details.