Testing with mocha and defining project with TS_NODE_PROJECT fails
See original GitHub issueHi
I have a project, a library, of small convenience methods for JS and I use Mocha for my unit testing with the following command:
cross-env TS_NODE_PROJECT=./test nyc mocha --compilers ts:ts-node/register ./test/*.spec.ts
Which has worked well until I updated to ts-node version 5.0.1.
Now it it has started failing with the error message that it cannot find my test folder.
So I wanted to know if you could tell me why it is failing?
Note
I use NYC (Istanbul) for code coverage, but the compilation fails even if I remove that from the command.
My Library set up
MyLibrary
|- test
| |- <*.spec.ts>
| |- tsconfig.json
|- <*.ts>
|- tsconfig.json
All my main files are in the root of the project and all the test files are in a test folder.
As I am compiling the tests a bit differently than the main files I have added a tsconfig.json in the root of the test folder and I am (trying to) instructing the TS compiler to use the test folder tsconfig.json for the compilation.
test/tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es2017",
"lib": ["es5", "es6", "dom"],
"moduleResolution": "node",
"allowJs": false,
"outDir": "."
},
"include": [
"../*.ts"
],
"exclude": [
"node_modules",
"../test/**/*.ts"
]
}
Error Message thrown
C:\projects\MyLibrary\node_modules\ts-node\src\index.ts:453
throw new TSError([formatDiagnostic(result.error, cwd, ts, 0)])
^
TSError: Γ¿» Unable to compile TypeScript
The specified path does not exist: 'C:\projects\MyLibrary\test'. (5058)
at readConfig (C:\projects\MyLibrary\node_modules\ts-node\src\index.ts:453:15)
at Object.register (C:\projects\MyLibrary\node_modules\ts-node\src\index.ts:209:18)
at Object.<anonymous> (C:\projects\MyLibrary\node_modules\ts-node\register\index.js:1:16)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at program.compilers.forEach.c (C:\projects\MyLibrary\node_modules\mocha\bin\_mocha:459:3)
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:\projects\MyLibrary\node_modules\mocha\bin\_mocha:451:19)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)

Top Related StackOverflow Question
You are definitely right, removing the space fixes it.
SET TS_NODE_PROJECT=./test/tsconfig.json&& mocha -r ts-node/register -r tsconfig-paths/register test/**/*.tsLast question why setting the inline option doesn’t work?
mocha -r ts-node/register --project /test/tsconfig.json -r tsconfig-paths/register test/**/*.tsBecause
--projectis not a mocha argument.