Run your TypeScript type tests using Jest.
Note that since v2
the jest-runner-tsd
is using tsd-lite
instead of tsd
. The type testing logic is the same in both implementations, i.e. both of them are wrapping around the TypeScript compiler to analyze types of your code.
The usage of tsd
with monorepos written in TypeScript may become cumbersome, because of checks unrelated with types. tsd-light
is an attempt to address these and similar issues.
Notable differences:
- The
tsd
configuration inpackage.json
is ignored. Please usejest.config
to configure discovery of your test files andtsconfig.json
to provide configuration for TS compiler. - The compiler configuration will be read from the nearest
tsconfig.json
for each test file. Hence, you may have a configuration for the whole project, or a group of test files, or just a particular test file. tsd-lite
will use the default TS compiler configuration without altering it. This means you should set"strict": true
to use strict assertions, which are the default ones in vanillatsd
.- Only type testing is performed without any additional checks or rules.
- The
printType
helper is discarded.
yarn add -D jest-runner-tsd @tsd/typescript
Remember to install @tsd/typescript
package. It is a required peer dependency.
- If your type tests live inside
__typetests__
folders, set upjest.config.tsd.js
like this:
module.exports = {
displayName: {
color: 'blue',
name: 'types',
},
runner: 'jest-runner-tsd',
testMatch: ['**/__typetests__/**/*.test.ts'],
};
-
Add
tsconfig.json
to your project with configuration for TS compiler. -
Run
yarn jest -c jest.config.tsd.js
command or just include a script inpackage.json
:
"scripts": {
"test:types": "jest -c jest.config.tsd.js"
}
To learn more about tsd
tests and assertions see the documentation.
MIT © Jest Community