Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukemorales/jest-type-matchers
Custom jest matchers to test the state of your types
https://github.com/lukemorales/jest-type-matchers
assertions custom-jest-matcher custom-matcher extend jest jest-matchers matchers test testing type-assertions type-testing types typescript
Last synced: 12 days ago
JSON representation
Custom jest matchers to test the state of your types
- Host: GitHub
- URL: https://github.com/lukemorales/jest-type-matchers
- Owner: lukemorales
- License: mit
- Created: 2022-10-08T18:31:05.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-10T02:24:46.000Z (about 2 years ago)
- Last Synced: 2024-12-04T20:53:59.643Z (17 days ago)
- Topics: assertions, custom-jest-matcher, custom-matcher, extend, jest, jest-matchers, matchers, test, testing, type-assertions, type-testing, types, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@lukemorales/jest-type-matchers
- Size: 115 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
jest-type-matchers
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:```dircolors
npm install --save-dev @lukemorales/jest-type-matchers
```## âš¡ Quick start
Import `@lukemorales/jest-type-matchers` once in your [tests setup
file](
https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array):```ts
// 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: ['/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`
```ts
expect(true).toHaveType();type Result = { ok: boolean } & { data: null };
expect({ ok: true, data: null }).toHaveType<{ ok: boolean; data: null }>();
```
This allows you to check that a variable has an expected type.#### `toNotHaveType`
```ts
expect('hello world').toNotHaveType();
```
This allows you to check that a variable does not have a specific type.#### `toHaveStrictType`
```ts
expect(true).toHaveStrictType();type Result = { ok: boolean } & { data: null };
expect({ 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`
```ts
expect('hello world').toNotHaveStrictType();
```
This allows you to check that a variable is not strict equal to a specific type.