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-helpers

v3.1.1

Published

TypeScript helper functions for Jest to help make your tests resilient to refactoring.

Downloads

136

Readme

jest-helpers

npm version Build Status Coverage Status

Benefits

  • When you rename a class, the name of your test for it will automatically update
  • When you rename any of the following, your test will get a TypeScript error until you update your test:
    • A method on a class
    • A field on a class
    • An exported function inside a module
  • Has useful functions for creating mocks:
    • partialOf<T>(partial: Partial<T>): T
    • deepPartialOf<T>(partial: DeepPartial<T>): T

Install

npm install jest-helpers --save-dev

Usage

./greeter.ts

export class Greeter {
  getGreeting(name: string) {
    return `Hello ${name}`;
  }
}

export function showGreeting(greeter: Greeter, name: string) {
  const greeting = greeter.getGreeting(name)
  console.log(greeting)
}

./greeter.test.ts

import { describeClass, describeFunction, describeMethod, partialOf } from 'jest-helpers';
import greeterModule = require('greeter');
import { Greeter, showGreeting } from './greeter';

describeClass(Greeter, () => {
  describeMethod(Greeter, 'getGreeting', () => {
    it('should return a personalised greeting', () => {
      const greeter = new Greeter();
      expect(greeter.getGreeting('Joe')).toEqual('Hello Joe');
    });
  });
});

describeFunction(greeterModule, 'showGreeting', () => {
  it('should log greeting to the console', () => {
    const greeterMock = partialOf<Greeter>({
      getGreeting: jest.fn().mockReturnValue('yo!')
    });

    jest.spyOn(console, 'log');

    showGreeting(greeterMock, 'Joe');

    expect(greeterMock.getGreeting).toHaveBeenCalledWith('Joe');
    expect(console.log).toHaveBeenCalledWith('yo!');
  });
});

Running jest --verbose will output something like

  example/greeter.ts
    Greeter
      getGreeting
        ✓ should return a personalised greeting (4ms)
    showGreeting
      ✓ should log greeting to the console (2ms)

If you rename your Greeter class, it will automatically update the test description.

If you rename your Greeter.getGreeting method, you will get a TypeScript error in your test until you update your test to match the new name.

If you rename your showGreeting function, you will get a TypeScript error in your test until you update your test to match the new name.

Contributing

Got an issue or a feature request? Log it.

Pull-requests are also welcome. 😸