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

jest-mock-proxy

v3.1.2

Published

Mock classes and objects with the power of proxies!

Downloads

15,973

Readme

jest-mock-proxy

buid buid version MIT License

Mock classes and objects with the power of proxies!

Creates a Proxy that will dynamically create spies when a property is accessed the first time. Every subsequent access will use the same spy. In combination with TypeScript this allows us to create a mock for any class/object without having to specify all its properties and methods.

tl;dr;

  1. The Proxy makes any property and method available on the mock at runtime.
  2. TypeScript limits access to properties and methods to the specified generic.

Install

Requires node 8+.

$ yarn add -D jest-mock-proxy

or

$ npm install -D jest-mock-proxy

Usage

Mock objects and instances

// service.ts
export class Service {
  foo() {
    console.log('hello');
  }
  bar(s: string) {
    return s;
  }
}

// some.test.ts
import { createMockProxy } from 'jest-mock-proxy';
import { service } from './service';

const mock = createMockProxy<typeof Service>();

mock.foo();

mock.bar.mockReturnValue('some string');
mock.bar('test'); // 'some string'

Example: Mock an elastic search client.

import { Client } from 'elasticsearch';
import { createMockProxy } from 'jest-mock-proxy';
import fixture from './__fixtures__/elastic-response.json';

// This is an imaginary service that depends on the elastic search client.
import createService from './createService';

const client = createMockProxy<Client>();
const service = createService(client);

beforeEach(() => {
  client.mockClear();
  client.search.mockResolvedValue(fixture);
});

test('use service to query', async () => {
  await service.query('https://example.com?q=hello');
  expect(client.search.mock.calls).toMatchSnapshot();
});

Mock a class and use jest's automock

When you need to mock a dependency via jest.mock, because you have no access to the module.

// query.ts
import { Pool, PoolConfig } from 'pg';

// 😨 This makes testing hard...
const pool = new Pool();

export const query = async (q: string, values?: any[]) => {
  // ...because how to mock this?
  const { rows } = pool.query(q, values);
  return rows;
};

// query.test.ts
import { Pool } from 'pg';
import { createProxyFromMock } from 'jest-mock-proxy';

import { query } from './query';

jest.mock('pg');
const mockedPool = createProxyFromMock(Pool);

test('you can now mock the pool.query', async () => {
  // Use mockedPool so you get good type inference from TS
  mockedPool.query.mockResolvedValue({ rows: [{ id: 1, data: 'data' }] });

  await query('SELECT * FROM table1'); // returns `[{ id: 1, data: 'data' }]`
});