JSPM

@lukemorales/jest-type-matchers

0.2.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 38
  • Score
    100M100P100Q74159F
  • License MIT

Custom jest matchers to test the state of your types

Package Exports

  • @lukemorales/jest-type-matchers
  • @lukemorales/jest-type-matchers/dist/index.js
  • @lukemorales/jest-type-matchers/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@lukemorales/jest-type-matchers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Testing tube emoji

jest-type-matchers

Latest build Latest published version Bundlephobia Tree shaking available Types included License Number of downloads GitHub Stars

Custom jest matchers to test the state of your types.

You write Typescript and want assert various things about the state of your types?
This library provides a set of custom matchers that you can use to extend jest
and assert your test results against expected types.

📦 Install

This library is available as a package on NPM, install with your favorite package manager:

npm install --save-dev @lukemorales/jest-type-matchers

⚡ Quick start

Import @lukemorales/jest-type-matchers once in your tests setup file:

// In your jest-setup.ts (or any other name)
import '@lukemorales/jest-type-matchers';

// In jest.config.js add (if you haven't already)
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts']

Custom Matchers

These custom matchers allow you to just check your types. This means that they will never fail your test suite because type-checking happens at compile-time only.

toHaveType

expect(true).toHaveType<boolean>();

type Result = { ok: boolean } & { data: null };

expect<Result>({ ok: true, data: null }).toHaveType<{ ok: boolean; data: null }>();

This allows you to check that a variable has an expected type.

toNotHaveType

expect('hello world').toNotHaveType<number>();

This allows you to check that a variable does not have a specific type.

toHaveStrictType

expect(true).toHaveStrictType<boolean>();

type Result = { ok: boolean } & { data: null };

expect<Result>({ ok: true, data: null }).toHaveStrictType<{ ok: boolean } & { data: null }>();

This allows you to check that a variable is strict equal to an expected type.

toNotHaveStrictType

expect('hello world').toNotHaveStrictType<number>();

This allows you to check that a variable is not strict equal to a specific type.