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

spy-on-async

v2.2.0

Published

async testing helpers for jest

Downloads

6

Readme

spy-on-async

Installation

npm install --save-dev spy-on-async

Usage

Designed to be used with jest.

setup:

Create a test setup file (or add to an existing one). This will ensure that a mocked method will not return a promise resolved from a previous test.

// ./path/to/resetAsyncMocks.ts
import {resetAllPromises} from "spy-on-async";

beforeEach(() => {
  resetAllPromises();
});
// jest.config.js
{
  "restoreMocks": true,
  "resetMocks": false,
  "clearMocks": false,
  "setupFilesAfterEnv": [
    "<rootDir>/path/to/resetAsyncMocks.ts,
  ]
}

resetMocks and clearMocks should be disabled by default. If they are enabled, this will remove the implementation from module mocks and make you sad.

spying on a method

// src/makeBananaBread.ts
import BananaMan from './BananaMan';

const makeBananaBread = async (numBananas: number) => {
  const bananas = await BananaMan.goBuyBananas(numBananas);

  return BananaMan.bakeBananaBread(bananas);
};

export default makeBananaBread;
// __tests__/makeBananaBread.spec.ts
import makeBananaBread from '../src/makeBananaBread';
import BananaMan from '../src/BananaMan';

describe('makeBananaBread', () => {
  it('calls some methods', async () => {
    const goBuyBananasSpy = spyOnAsync(BananaMan, 'goBuyBananas');
    const bakeBananaBreadSpy = spyOnAsync(BananaMan, 'bakeBananaBread');

    spyOnAsync(BananaMan, 'bakeBananaBread');

    const promise = makeBananaBread(2);

    expect(BananaMan.goBuyBananas).toHaveBeenCalledWith(2);

    await goBuyBananasSpy.mockResolveNext('bananas');
    // awaiting ensures the method continues execution before we do our next assertion

    expect(BananaMan.bakeBananaBread).toHaveBeenCalledWith('bananas');

    await bakeBananaBreadSpy.mockResolveNext('banana bread');

    expect(await promise).toEqual('banana bread');
  });
});

testing branching possibilities

// __tests__/makeBananaBread.test.js
import makeBananaBread from '../src/makeBananaBread';
import ModuleWithAsyncStuff from '../src/ModuleWithAsyncStuff';

describe('makeBananaBread', () => {
  let promise: Promise<BananaBread>;
  let goBuyBananasSpy: AsyncSpy<Banana>;
  let bakeBananaBreadSpy: AsyncSpy<BananaBread>;

  beforeEach(() => {
    goBuyBananasSpy = spyOnAsync(BananaMan, 'goBuyBananas');
    bakeBananaBreadSpy = spyOnAsync(BananaMan, 'bakeBananaBread');

    promise = makeBananaBread(2);
  });

  it('buys some bananas', async () => {
    expect(BananaMan.goBuyBananas).toHaveBeenCalledWith(2);
  });

  describe('when store has bananas', () => {
    beforeEach(async () => {
      await goBuyBananasSpy.mockResolveNext('bananas');
    });

    it('bakes those bananas', async () => {
      expect(BananaMan.bakeBananaBread).toHaveBeenCalledWith('bananas');
    });
  });

  describe('when store is out of bananas', () => {
    beforeEach(async () => {
      await goBuyBananasSpy.mockRejectNext('no bananas');
    });

    it('does not bake them bananas', async () => {
      await expect(promise).rejects.toBeDefined();
      expect(BananaMan.bakeBananaBread).not.toHaveBeenCalled();
    });
  });
});

Mocking a module

createAsyncMock can be used in jest manual mocks

import { createAsyncMock } from 'spy-on-async';

export const someMethod = createAsyncMock<ResolveType>();

Appendix

Accessing async spy from module

Though the examples above save the spy to a variable, you can also access the spy via the spied module:

await MockedModule.asyncMethod.mockResolveNext(value)

If you aren't writing typescript, this way is fluent and makes clear which module has been spied on. However, in typescript you need to cast the method as a mock. For whatever reason, it is necessary to first cast to jest.Mock. If anyone can fix this, I will give them a big ol hug.

await ((MockedModule.asyncMethod as jest.Mock) as AsyncMock).mockResolveNext(value)