I’d like to keep my source code and tests separate in my Typescript project:
packages/
torgler/
package.json
tsconfig.json
src/
FlidgetTorgler.tsx
WingleTorgler.tsx
test/
FlidgetTorgler.test.tsx
WingleTorgler.test.tsx
Since I don’t want anything in the test
folder to be visible outside the package, or to ever get built, it makes sense that the tsconfig
rootDir should just be .src/
:
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"rootDir": "./src",
"composite": true
},
"include": ["src/**/*"]
}
Inside FlidgetTorgler.test.tsx
, I now need to import from the ../src
folder:
// FlidgetTorgler.test.tsx
import React from 'react'
import FlidgetTorgler from '../src/FlidgetTorgler'
... test stuff here ...
This all works fine. But Typescript won’t let me import from anything inside test/
, since it’s not from an included file:
// FlidgetTorgler.test.tsx
import React from 'react'
import FlidgetTorgler from '../src/FlidgetTorgler'
import someTestUtil from './utils' // ✗ ERROR: File './utils' is not under 'rootDir'
The include directive looks like what I want, and the example in the Typescript docs looks almost exactly like what I’m trying to do:
...
{
"include": ["src/**/*", "test/**/*"]
}
But this doesn’t work. I still get the same error, because test/
is not under rootDir
. Even though include
looks like you can put whatever you want in there, it seems to only pick up things under the rootDir
.
I can change rootDir
to be .
, or I can use rootDirs instead, but both of those options break everything upstream that imports from torgler
and I haven’t gotten them to work with the rest of my project.
(This question is similar to Typescript configuration with src and test, but that has some extraneous details and I’m not sure the answer there is my best choice in any case.)
What’s the best way to keep src/
and test/
folders separate like this?