Coder Social home page Coder Social logo

lukemorales / jest-type-matchers Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 0.0 118 KB

Custom jest matchers to test the state of your types

Home Page: https://www.npmjs.com/package/@lukemorales/jest-type-matchers

License: MIT License

Shell 1.43% JavaScript 2.49% TypeScript 96.09%
assertions custom-matcher extend jest jest-matchers matchers test testing type-assertions type-testing

jest-type-matchers's Introduction

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.

jest-type-matchers's People

Contributors

lukemorales avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.