Reputation: 3850
In my TypeScript project that's running tests via ts-node and jasmine I have defined some paths in tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"module": "commonjs",
"lib": [
"es5",
"es2015",
"DOM"
],
"moduleResolution": "Node",
"outDir": "./dist/cjs",
"declaration": true,
"sourceMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"removeComments": true,
"strict": true,
"paths": {
"@common": ["common/index"],
"@crashes": ["crashes/index"]
}
},
"files": [
"./src/index.ts"
]
}
I am able to import via paths @common
and @crashes
in my production code, but for some reason VS Code gives me the error Cannot find module '@common' or its corresponding type declarations.
in my spec.ts files.
How can I get VS Code to recognize TypeScript paths in my tests?
Upvotes: 2
Views: 342
Reputation: 3850
This is because VS Code's intellisense will follow the rules in tsconfig.json
. In this instance, I was not including the spec.ts
files in the TypeScript compilation. Additionally, I did not want to include the TypeScript files in my project's build output.
The solution was as follows:
files
property from tsconfig.json
tsconfig.dist.json
that extends tsconfig.json
and add the files
property here{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist/cjs",
},
"files": [
"./src/index.ts"
]
}
tsconfig.dist.json
in the publish scriptAdditionally, once I did all this, I got the following error when I tried to run jasmine tests with ts-node:
> ts-node node_modules/jasmine/bin/jasmine --config=spec/support/jasmine.spec.json
Error: Cannot find module '@common'
The solution to this error was to install tsconfig-paths and update my test
script in package.json
{
"scripts": {
"test": "ts-node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine --config=spec/support/jasmine.spec.json"
}
}
Upvotes: 1