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

typed-jest-expect

v1.0.1

Published

Strongly typed jest expect

Downloads

3,759

Readme

typed-jest-expect

Elegant jest.expect typings for a more civilized age

Why?

By default, the expect utility of jest is very broadly typed, which makes sense for a testing library, and doesn't complain when you do something like that:

expect(42).toEqual("definitely not 42");

However, when your codebase is mostly well-typed internally, you don't benefit from TypeScript as much as you could, specifically:

  1. static warnings for obvious errors suchs as typos,
  2. editor auto-completion.

This missed opportunity impedes productivity.

This library re-exports expect with much stronger typings: you can only test equality with assignable values, use function assertions with Jest mocked functions, use number comparison with numbers and BigInts, etc.

It prevents you from writing code such as:

tExpect(42).toEqual("definitely not 42");
tExpect({
  x: 42,
}).toEqual({
  y: 77,
});

And conversely, auto-completion now works correctly:

tsExpect({
  foo: "bar",
  fizz: "buzz",
}).toMatchObject({
  fizz: "bu...", // auto-complete
});

Installation

npm i typed-jest-expect
import { tExpect } from "typed-jest-expect";

tExpect([1, 3, 2]).toContain(3);

This library has virtually 0 runtime cost. The emitted JS code does nothing but re-exporting the global expect function:

"use strict";
Object.defineProperty(exports, "__esModule", {
  value: true,
});
exports.tExpect = void 0;
const tExpect = (t) => expect(t);
exports.tExpect = tExpect;

Since it doesn't change the original expect typings, you can still use the broadly-typed original expect for interacting with code that is not fully typed:

tExpect(42).toEqual(42);
expect(notSureWhatThisShouldReturn()).toEqual("1337");

Examples of invalid code statically rejected by tExpect

// Calling .resolves or .rejects on non-promise
tExpect(0).resolves; // never
tExpect(Promise.resolve(0)).resolves.toEqual(0);

// Calling toBe / toEqual / toMatchObject with incompatible values
tExpect({ foo: "bar" }).toEqual({ foo: 42 } /* never */);

// toBeCalled on anything that is not a mocked function
tExpect(42).toBeCalled; // never
tExpect(() => 42).toBeCalled; // never
tExpect(jest.fn(() => 42)).toBeCalled();

// toBeCalledWith with non-assignable parameters
const sum = jest.fn((a: number, b: number): number => a + b);
tExpect(sum).toBeCalledWith("a" /* never */, 3);

// toHaveReturnedWith with non-assignable return value
tExpect(sum).toHaveReturned(null /* never */);

// toHaveLength with anything that doesn't have a .length number property
tExpect({}).toHaveLength; // never

// toHaveProperty with a non-existent property key
tExpect({ foo: "bar" }).toHaveProperty("fizz", "bar" /* never */);

// toBeCloseTo, toBeGreaterThan, etc., with non-number / BigInt
tExpect("32").toBeGreaterThan; // never

// toBeDefined on non-optional types
tExpect(42).toBeDefined; // never
tExpect(true ? 42 : undefined).toBeDefined(); // ok

// toBeInstanceOf on non-assignable constructors
class Animal {
  kind: string;
  constructor(kind: string) {
    this.kind = string;
  }
}
tExpect({ species: "dog" }).toBeInstanceOf(Animal /* never */);

// toBeNull on non-nullable types
tExpect(42).toBeNull; // never
tExpect(true ? 42 : null).toBeNull(); // ok (throws though)

// toThrow on non-functions, or functions with parameters
tExpect((a: number, b: number) => {}).toThrow; // never