I am trying to run my tests with mocha and ts-node compiler.
I have a src folder with my code and test with a few tests.
This is my tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "ES6",
"outDir": "dist",
"rootDir": "src",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
I am running the following command: mocha --compilers ts:ts-node/register test/**.ts
And when i do, i get this:
TSError: ⨯ Unable to compile TypeScript...
is not under 'rootDir'...
'rootDir' is expected to contain all source files. (6059)
Is there any easy way to circumvent this issue without having all the source code and the tests under the src folder?
I solved this by changing rootDir to rootDirs: ["src","test"], and it worked.
We could probably override rootDirs, but I'm not sure if that would introduce any odd side-effects in terms of TypeScript compilation. If someone knows, let me know. /cc @mhegazy
Having rootDir and rootDirs is silly but it is not your fault.
why are you using rootDirs? I am not sure i see how this is relevant to the error you saw. here is more information about what rootDirs mean:http://www.typescriptlang.org/docs/handbook/module-resolution.html#virtual-directories-with-rootdirs
Any reason why switching from "rootDir" to "rootDirs" worked? I had the same issue with webpack.config.ts until I switched.
Most helpful comment
I solved this by changing
rootDirtorootDirs: ["src","test"],and it worked.