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 🙏

© 2025 – Pkg Stats / Ryan Hefner

safe-mock

v0.2.11

Published

Type Safe Mocking Library written for Typescript and Javascript

Readme

Safe Mock

Build Status Chat

Safe Mock is a library for Typescript (or Javascript) that allows mocks to be created from typescript interfaces or classes and utilized with simple typesafe syntax.

The syntax is inspired by Mockito.

Check out the examples below to get started:

Quick Start

npm install safe-mock

Then import it in your tests:

import SafeMock, {when, verify} from "safe-mock";

Create your first Safe Mock like this:

interface SomeService {
    generate(key: string): string;
}

const mockSomeService = SafeMock.build<SomeService>();

Configure the mock to return values using SafeMock's when

when(mockSomeService.mockSomeService("key")).return("generatedKey");
when(mockSomeService.mockSomeService("other")).return("otherGeneratedKey");

Assert that mocks were used correctly with SafeMock's verify

verify(mock.mockSomeService).calledWith("key");

Gotchas

  • SafeMock requires TypeScript 2.1 or later.
  • SafeMock requires a Javascript runtime with support for the proxy object.
  • If running on Node.js version 6+ is required.

The Details

Creating mocks

Mocks can be created from an interface, class, or functions.

Creating mocks of interfaces
import SafeMock, {Mock} from "safe-mock";

interface SomeService {
    createSomething(argument: number): string;
}

// mock an interface by passing the type of the mock as a generic to the build method  
const mock: Mock<SomeService> = SafeMock.build<SomeService>();

// Typescript will infer the type so you don't need to specify the type of the mock
const mock = SafeMock.build<SomeService>();
Creating Mocks of classes
import SafeMock, {Mock} from "safe-mock";

class SomeServiceClass {
    createSomething(argument: number) {
        return 106;
    }
}

// mock a class by passing the class into the build method
const mock: Mock<SomeServiceClass> = SafeMock.build(SomeServiceClass);

// mock a class by passing the type of the mock as a generic to the build method  
const mock: Mock<SomeServiceClass> = SafeMock.build<SomeServiceClass>();

//Typescript will infer the type so you don't need to specify the type of the mock
const mock = SafeMock.build<SomeService>();
Creating Mocks of functions
import SafeMock, {Mock} from "safe-mock";

function someFunc() {
} 

// Mock a function by passing it into the mockFunction method 
const mockFunction = SafeMock.mockFunction(someFunc);

// if you have a type signature for a function you can use generics to create a mockFunction
type FunctionToMock = () => string;
const mockFunction = SafeMock.mockFunction<FunctionToMock>();

// you can pass a name into the mockFunction method to make debugging easier
type FunctionToMock = () => string;
const mockFunction = SafeMock.mockFunction<FunctionToMock>("nameOfFunc");

Specifying behavior for mocks

Setting Return Values
import SafeMock, {Mock, when} from "safe-mock";

interface SomeService {
    someMethod(argument: number, otherArg: string): string;
}
const mock: Mock<SomeService> = SafeMock.build<SomeService>();

// specify return values only when mocks are called with certain arguments like this
when(mock.someMethod(123, "some arg")).return("expectedReturn"); 

// if you don't care what arguments a mock receives you can specify a return value for all calls
when(mock.someMethod).return("expectedReturn"); 
Making mocks throw Exceptions
import SafeMock, {Mock, when} from "safe-mock";

interface SomeService {
    someMethod(argument: number, otherArg: string): string;
}
const mock: Mock<SomeService> = SafeMock.build<SomeService>();

// specify thrown exceptions only when mocks are called with certain arguments like this
when(mock.someMethod(123, "some arg")).throw(new Error("BRR! Its cold!")); 

//if you don't care what arguments a mock receives you can specify a thrown exceptions for all calls
when(mock.someMethod).throw(new Error("BRR! Its cold!")); 
Working with mocks that return a Promise
import SafeMock, {Mock, when} from "safe-mock";

interface SomeService {
    someMethod(argument: number): Promise<string>;
}
const mock: Mock<SomeService> = SafeMock.build<SomeService>();

// specify that the mock returns rejected promises with a rejected value with reject
when(mock.someMethod(123)).reject(new Error("BRR! Its cold!"));

mock.someMethod(123); //returns Promise.reject(new Error("BRR! Its cold!"));

// specify that the mock returns resolved promises with resolve
when(mock.someMethod(124)).resolve("Hooray! You passed in 124"); 

mock.someMethod(124); //returns Promise.resolve("Hooray! You passed in 124");

Note: resolve() and reject() are only available on mocks that return a Promise.

Verifying behavior with mocks

Verify that the correct arguments were used
import SafeMock, {verify} from "safe-mock";

interface SomeService {
    someMethod(argument: number, otherArg: string): string;
}
const mock = SafeMock.build<SomeService>();

//use verify.calledWith to check the exact arguments to a mocked method
verify(mock.someMethod).calledWith(123, "someArg");

//use verify.called() to check that a mock was called at least once
verify(mock.someMethod).called();
Verify that mocks were never called
import SafeMock, {verify} from "safe-mock";

interface SomeService {
    someMethod(argument: number): string;
}
const mock = SafeMock.build<SomeService>();

//use verify.never to check that a method was never called with any arguments.
verify(mock.someMethod).never.called();

//use verify.never.calledWith to check that a mock was never called with specific arguments
verify(mock.someMethod).never.calledWith(123);
Working with optional parameters
import SafeMock, {verify} from "safe-mock";

interface SomeService {
    someMethod(argument?: number, otherArg?: string): string;
}
const mock = SafeMock.build<SomeService>();

//use verify.unsafe when working with optional parameters
//unfortunately, type safety is lost when using 'verify.unsafe'  
verify.unsafe(mock.someMethod).calledWith(123, "someArg");
verify.unsafe(mock.someMethod).calledWith(123);
verify.unsafe(mock.someMethod).called();

Reset-ing mocks

Use resetMock() to reset call information and mocked behavior
import SafeMock, {verify} from "safe-mock";

interface SomeService {
    someMethod(argument: number, otherArg: string): string;
    someOtherMethod(): string;
}
const mock = SafeMock.build<SomeService>();

//use resetMock() to a reset all methods on a mock
mock.resetMock()

//use resetMock() on an individual method to only reset that method
mock.someOtherMethod.resetMock()

Use mock functions just like mocked methods.

import SafeMock, {verify, when} from "safe-mock";

function someFunction() {
    return "string"
}

const mockFunction = SafeMock.mockFunction(someFunction);

when(mockFunction).return("return vlause");
verify(mockFunction).called()
someOtherMethod.resetMock();

Rely On the Typescript Compiler to prevent mistakes

SafeMock won't let you return the wrong type from mocks.

interface SomeService {
    createSomething(): string;
}

const mock: Mock<SomeService> = SafeMock.build<SomeService>();

//Won't compile createSomething returns a string
when(mock.createSomething()).return(123); 

SafeMock won't let you verify arguments of the wrong type.

interface SomeService {
    createSomethingWithAString(argument: string): string;
}

const mock: Mock<SomeService> = SafeMock.build<SomeService>();

mock.createSomethingNoArgs();

//Won't compile, createSomething takes a string
verify(mock.createSomethingNoArgs).calledWith(123); 

SafeMock won't let you verify the wrong number of arguments.

interface SomeService {
    createSomethingNoArgs(): string;
}

const mock: Mock<SomeService> = SafeMock.build<SomeService>();

mock.createSomethingNoArgs();

//Won't compile, createSomething takes no args
verify(mock.createSomethingNoArgs).calledWith(123); 

SafeMock won't let you verify non mocked methods.

const notAMock = {
    blah() {}
};

//Won't compile notAMock.blah is not a mock
verify(notAMock.blah).called();