I am working on a large project that we are using Typescript 3.0 project references. We are using a similar project structure to the accepted comment of this article How to use project references in TypeScript 3.0?. For a testing framework we have chosen Jest, and have used Jest's projects feature to separate unit, integration and linting tests.
"projects": [
{
"displayName": "integration",
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": [
"<rootDir>/test/integration/**/*.spec.ts"
],
"setupFilesAfterEnv": [
"<rootDir>/test/jest-setup.ts"
]
},
{
"displayName": "unit",
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": [
"<rootDir>/test/unit/**/*.spec.ts"
],
"setupFilesAfterEnv": [
"<rootDir>/test/jest-setup.ts"
]
},
{
"displayName": "lint",
"runner": "jest-runner-eslint",
"testEnvironment": "node",
"testMatch": [
"<rootDir>/src/**/*.ts"
]
}
]
So, using project references; I can build src, test, and the root directory projects using tsc --build
. However when using Jest I am running into compilation issues as Jest cannot find the definition files from the src project. As I said, this can be compiled using the typescript compiler and has no linting issues in VSCode.
One way I have tried to bandaid fix this is by importing the definition files from src into the jest setup file, ie: import '../src/index.d'
. This fixes the issue only for the first run after clearing Jest's cache. The second and following runs will all then fail with compilation issues.
Has anyone been successful in using Jest with Typescript's project references? Does Jest even support project references? Why does clearing the cache fix this issue only for the first run, and not any other following runs?