File Structure
This page covers several project structure strategies. Pick the one that suits your needs.
Dedicated Directory
Keeping type test files in a dedicated directory is the recommended approach. It separates concerns and makes isolation straightforward.
- MethodLikeKeys.tst.ts
- tsconfig.json
- MethodLikeKeys.ts
- package.json
- tsconfig.json
The root tsconfig.json holds the compiler configuration of the project. Extend from it and reuse the configuration for type testing:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"strict": true,
"types": []
},
"include": ["./"],
"exclude": []
}This configuration prevents loading global scope typings from @types/* packages. It includes only the files needed for type testing.
By convention, unit tests are suffixed with .test.* and type tests with .tst.*. The latter ones are only statically analyzed and are not meant to be executed, hence the missing e in the suffix.
Shared Directory
Unit and type test files can also share a single directory:
- MethodLikeKeys.tst.ts
- secondItem.test.ts
- tsconfig.json
- MethodLikeKeys.ts
- secondItem.ts
- package.json
- tsconfig.json
- tstyche.json
Keep the TSConfig as focused as possible to avoid global typings leaking into your type tests:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"strict": true,
"types": [] // prefer explicitly importing testing APIs
},
"include": ["./"],
"exclude": []
}Next to Source Files
Isolation of type tests placed next to source files requires a bit more configuration. A project structure could look like this:
- MethodLikeKeys.ts
- MethodLikeKeys.tst.ts
- secondItem.ts
- secondItem.test.ts
- package.json
- tsconfig.json
- tsconfig.test.json
- tstyche.json
The references field can be used to define the project in the root tsconfig.json:
{
"compilerOptions": {
// ...
"composite": true
},
"include": ["./src"],
"exclude": ["**/*.test.*", "**/*.tst.*"],
"references": [{ "path": "./tsconfig.test.json" }]
}tsconfig.test.json can then extend it:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"strict": true,
"types": [] // prefer explicitly importing testing APIs
},
"exclude": []
}