All Projects → jest-community → jest-runner-tsd

jest-community / jest-runner-tsd

Licence: MIT License
Jest runner to test typescript typings

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

jest-runner-tsd

Run your TypeScript type tests using Jest.

version license

Note: the jest-runner-tsd is using tsd-lite instead of tsd. Both of them have the same type testing logic, but tsd-light makes it easier to test projects written in TypeScript (or types generated by your library).

Most important differences (for the full list see tsd-lite repo):

  • tsd-lite has no additional rules or checks;
  • jest.config is used to discover test files;
  • and tsconfig.json provides configuration for TS compiler. For details see Configuration section.

Install

yarn add --dev jest-runner-tsd @tsd/typescript
# or
npm install --save-dev jest-runner-tsd @tsd/typescript

Remember to install @tsd/typescript package. It is a required peer dependency.

Note that @tsd/typescript will be used to compile type tests. Generally it is recommended to match versions of @tsd/typescript and typescript in a project, but you may choose to test on different version too.

Configuration

Jest

First of all, you should configure Jest to discover your test files. For example, if the files have .test.ts suffix and live inside __typetests__ directories, set up jest.config.tsd.js like this:

module.exports = {
  displayName: {
    color: 'blue',
    name: 'types',
  },
  runner: 'jest-runner-tsd',
  testMatch: ['**/__typetests__/*.test.ts'],
};

TS Compiler

Your test files will be compiled using the TypeScript compiler (similar to tsc), but, instead of emitting JS code, tsd-lite will analyze types and diagnostics returned by the compiler.

To compile each test file, tsd-lite will read the nearest tsconfig.json and will pass the configuration to the compiler. Hence, you may have a configuration for the whole project, or a group of test files, or just a particular test file.

For example, if your project already includes a tsconfig.json in the root directory, but you prefer to have different configuration for testing, simply add another tsconfig.json to a directory with the test files. It may override or extend your root configuration.

Hint: run yarn tsc -p path/to/__typetests__ --showConfig to print the configuration which applies to the test files.

Note: if tsconfig.json is not found, the compiler will fall back to the default configuration.

Optionally Strict

Just like TypeScript, tsd-lite is optionally strict. In contrary, the vanilla tsd is strict by default. If you are migrating your test suite, remember to set "strict": true in tsconfig.json.

import { expectType } from 'tsd-lite';

declare const loggedInUsername: string;

const users = [
  { name: 'Oby', age: 12 },
  { name: 'Heera', age: 32 },
];

const loggedInUser = users.find(u => u.name === loggedInUsername);

expectType<number>(loggedInUser.age);

The assertion in this example fails with "strict": true, but passes with "strict": false.

Running Tests

If all is set, simply run yarn jest -c jest.config.tsd.js command. Or better include a script in package.json:

"scripts": {
  "test:types": "jest -c jest.config.tsd.js"
}

Learn More

To learn more about tsd and its assertions see the documentation.

License

MIT © Jest Community

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].