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

ts-mockery

v1.2.0

Published

Yet another typescript mocking library.

Downloads

131,395

Readme

npm version Build Status Test Coverage Maintainability

ts-mockery

Simple type-safe mocking library for TypeScript.

StackBlitz Examples

Why use this?

  • Usable with either Jasmine or Jest.
  • The primary focus is type-safety.
  • Spies are setup for mocked functions.
  • Nested objects and arrays can be mocked with type-safety

How do I use this?

To create a Mock:

import { Mock } from 'ts-mockery';

interface ObjectToNest {
  anotherStringFunction: () => string;
  unusedFunction: () => void;
}

class ObjectToMock {
  nestedObject: ObjectToNest;
  string = ':-)';
  stringFunction = (buzz: string): string => buzz.toUpperCase();
}

Mock.of<ObjectToMock>({ string: 'not :-)' });
Mock.of<ObjectToMock>({ string: 'still not :-)', stringFunction: () => 'type-safe partial of return type' });
Mock.of<ObjectToMock>({ nestedObject: { anotherStringFunction: () => 'type-safe partial of return type' } });

To update a Mock:

import { Mock } from 'ts-mockery';

interface ObjectToNest {
  anotherStringFunction: () => string;
  unusedFunction: () => void;
}

class ObjectToMock {
  nestedObject: ObjectToNest;
  string = ':-)';
  stringFunction = (buzz: string): string => buzz.toUpperCase();
}

const mock = Mock.of<ObjectToMock>({ string: 'not :-)' });
Mock.extend(mock).with({ string: 'still not :-)', stringFunction: () => 'type-safe partial of return type' });

More usage examples can be found @ https://stackblitz.com/edit/ts-mockery-examples?file=tests.ts

To mock a static method:

import { Mock } from 'ts-mockery';

class ObjectToMock {
  static static: () => 'hi';
}

Mock.staticMethod(ObjectToMock, 'static', () => 'not hi');

Mock.from:

We noticed issues when we gave instantiated base class objects to Mock.of. The reason being that the type checker does not look at the prototype.

import { Mock } from 'ts-mockery';
Mock.of<SomeClass>(new BaseOfSomeClass());

now with Mock.from:

import { Mock } from 'ts-mockery';
Mock.from<SomeClass>(new BaseOfSomeClass());

Also when setting mocked property to an observable there is a circular reference that would throw an RangeError {}

import { Mock } from 'ts-mockery';
import { of } from 'rxjs';
Mock.of<SomeClass>({ something$: of(someValue) });

now with Mock.from:

import { Mock } from 'ts-mockery';
import { of } from 'rxjs';
Mock.from<SomeClass>({ something$: of(someValue) });

To mock a function you do not care about:

We got you covered, Mock.noop will return you a spied on function.

import { Mock } from 'ts-mockery';

class ObjectToMock {
  doNotCare: () => 'hi';
}

const mock = Mock.of<ObjectToMock>({ doNotCare: Mock.noop });
const result = mock.doNotCare();

expect(mock.doNotCare).toHaveBeenCalled();
expect(result).not.toBe('hi');

To mock an object you do not care about:

With Mock.all it will use a proxy to create spies as on demand.

import { Mock } from 'ts-mockery';

class ObjectToMock {
  doNotCare: () => 'hi';
}

const mock = Mock.all<ObjectToMock>();
const result = mock.doNotCare();

expect(mock.doNotCare).toHaveBeenCalled();
expect(result).not.toBe('hi');

How do I configure this?

Create a test setup file to be included in your Jest or Jasmine config.

import { Mock } from 'ts-mockery';

// The argument to configure can be either jest, jasmine, noop, or an object that implements the exported SpyAdapater interface
Mock.configure('jest');

The above can be added directly to your karma test shim if you'd like.

To configure in Jest add the mockery configuration into the jest config with the key "setupFiles" like so:

  setupFiles: ['./jest-setup.ts'],

It is important that this file is included before tests run.

Also Important:

Jest for whatever reason does not reset mocks between tests by default. This causes problems with the mocking of static methods. If you intend to use static method mocking you can add "restoreMocks: true" to your jest config and all will be right in the world.