JSPM

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

The easiest way to test your TS types with Jest

Package Exports

  • jest-tsd
  • jest-tsd/index.js

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 (jest-tsd) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Jest TSD

The easiest way to test your TS types with Jest.

Install

# Install with npm
npm i -D jest-tsd @tsd/typescript

# Or install with yarn
yarn add --dev jest-tsd @tsd/typescript

Note: @tsd/typescript will be used to compile your type tests. If you have compiling issues, adjust its version to match the version of typescript you have installed.

Setup

  1. Create a type definition test file .test-d.ts in the same directory with the same name as your Jest test file

    • e.g. If your Jest test is located at src/dir/foo.test.js, create a src/dir/foo.test-d.ts file

    • In your type definition test you can import the assertion functions from jest-tsd

      import { expectType } from 'jest-tsd';
  2. Add a test to your Jest test file

    import { expectTypeTestsToPassAsync } from 'jest-tsd';
    
    it('should not produce static type errors', async () => {
      await expectTypeTestsToPassAsync(__filename);
    });

    If for some reason your type definition test file(s) are not co-located to your Jest test file, you can pass absolute path(s) to them to expectTypeTestsToPassAsync() instead of __filename.

Assertions

These assertions are re-exported from tsd.

expectType<T>(expression: T)

Asserts that the type of expression is identical to type T.

expectError<T = any>(expression: T)

Asserts that expression throws an error.

expectNotType<T>(expression: any)

Asserts that the type of expression is not identical to type T.

expectAssignable<T>(expression: T)

Asserts that the type of expression is assignable to type T.

expectNotAssignable<T>(expression: any)

Asserts that the type of expression is not assignable to type T.

expectDeprecated(expression: any)

Asserts that expression is marked as @deprecated.

expectNotDeprecated(expression: any)

Asserts that expression is not marked as @deprecated.