Sub topic 13
Separate projects for tests and tooling
This chapter shows how to move test files and tool configuration out of your source projects and into their own projects. It assumes you can already do the following:
- Write a composite project and a
referencesentry (earlier chapters). - Run
tsc --build. - Use a test runner that executes TypeScript directly, such as Vitest.
Why tests and tooling get their own projects
By default, a project includes everything under its folder. If packages/core keeps its tests in src/, four things go wrong:
- Emitted tests. Test files are compiled into
distand published with the library. - Leaked globals. Ambient types such as test-runner globals become visible to production code. A stray
describeorprocessin a library file type-checks when it should not. - Fixed options. Tests and source share one set of compiler options. Tests often need different ones, for example looser
moduleResolutionfor a bundler-based runner. - Needless rebuilds. Editing a test invalidates the library's
.tsbuildinfo, so everything that depends on the library is checked again.
Tooling files have a similar problem. A vitest.config.ts or eslint.config.ts at the repository root runs in Node, not in your app's environment. It should not be compiled with the app's lib and types.
A separate project for each concern fixes this. Each project gets its own include list, its own options, and its own place in the dependency graph.
Layout for a test project
Put the tests in their own folder with their own tsconfig.json. Keep the source project's include limited to src.
packages/core/
tsconfig.json (source project, include: ["src"])
src/
greet.ts
test/
tsconfig.json (test project)
greet.test.tsThe test tsconfig.json sits next to the test files on purpose. The editor picks the nearest tsconfig.json when it walks up from an open file. If the test file's nearest config were packages/core/tsconfig.json, which does not include test/, the editor would treat the file as part of no project. You would lose consistent options and get confusing errors. A config in test/ avoids that.
First, tighten the source project so that no ambient types leak in. This is the packages/core/tsconfig.json from earlier, with one new line:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist",
"types": []
},
"include": ["src"]
}"types": [] stops TypeScript from automatically including every @types/* package it finds. The library sees only the types it imports explicitly.
Now the test project, packages/core/test/tsconfig.json:
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"noEmit": true,
"rootDir": "."
},
"include": ["."],
"references": [{ "path": ".." }]
}Four things to note:
extendsgoes up three levels, because the file is now three folders below the repository root.noEmitis set because the runner executes the TypeScript directly. This project exists only to type-check. Nothing references it, so it does not needcomposite. UsingnoEmittogether withtsc --buildneeds TypeScript 5.6 or later. On older versions, runtsc --noEmit -p packages/core/testafter a build.referencespoints at.., the source project. A reference path can be a folder that holds atsconfig.jsonor a path to a config file.includecovers only the test folder.
The test itself imports the source with an ordinary relative path:
import { expect, test } from "vitest";
import { greet } from "../src/greet.js";
test("greet builds a greeting", () => {
expect(greet("tests")).toBe("Hello, tests!");
});When the test project is type-checked, the compiler sees that ../src/greet.js belongs to a referenced project. It reads the declaration output in packages/core/dist instead of checking the source again. At run time, Vitest resolves the same import to greet.ts and runs the TypeScript directly. The two paths do not conflict, because the type-check never runs your code.
A tooling project for config files
Config files such as vitest.config.ts run in Node, so they need Node types and a Node-style module setup. Give them a project at the repository root. Name it tsconfig.tooling.json, so the root tsconfig.json stays free for the solution file:
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"noEmit": true,
"types": ["node"]
},
"include": ["vitest.config.ts", "eslint.config.ts"]
}This project references nothing, because config files do not import your packages. It needs @types/node installed. If an include entry matches no file, the compiler reports an error, so list only files that exist.
Some teams give tooling configs different options from the base. A config that a bundler-based tool loads might need "moduleResolution": "Bundler". Setting that in the tooling project keeps it away from the library's NodeNext rules, which is the main benefit of a separate project.
Adding the new projects to the solution
The solution file from the earlier chapter lists every project. Add the two new ones:
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/core/test" },
{ "path": "./packages/app" },
{ "path": "./tsconfig.tooling.json" }
]
}tsc --build now does the following:
- Builds
core. - Builds
app, which depends oncore. - Type-checks the core tests.
- Type-checks the tooling configs.
The order among unrelated projects does not matter. The only guarantee is that core finishes before anything that references it.
The solution file also helps the editor. When you open vitest.config.ts, the editor sees that the root tsconfig.json has files: [] and a list of references. It looks through those references for a project that contains the file, and finds tsconfig.tooling.json. This works only if the tooling project is listed in the solution.
Running it
# Build the libraries, then type-check tests and tooling
tsc --build
# Run the tests (the runner reads TypeScript directly)
npx vitest runType checking and test execution are separate steps. A test can pass at run time and still fail the type check, because the runner usually strips types without checking them. In CI, run both steps.
Common errors
| Error | Cause | Fix |
|---|---|---|
| TS6305: Output file `.../dist/greet.d.ts` has not been built from source file | You ran `tsc -p test` before `core` was built | Run `tsc --build`, which builds `core` first |
| TS6306: Referenced project must have setting "composite": true | The test project references a project that is not composite | Add `composite` to the referenced project |
| TS6307: File is not listed within the file list of project | A composite project imports a file that its `include` does not match | Widen `include` or move the file |
| TS6059: File is not under `rootDir` | `rootDir` is `src` but a test file is included in the same project | Move tests to their own project |
| Test globals such as `describe` are visible in `src` | The source project lacks `"types": []` | Add `"types": []` to the source project |
The TS6307 error is the one most often caused by test files. A source project that has no include falls back to the default, which covers the whole folder, so it picks up test/*.ts and its rootDir: "src" is then violated. An explicit include: ["src"] prevents this.
Exercise
Exercise
Your packages/core/tsconfig.json has no include field and sets "rootDir": "src". After you add packages/core/test/greet.test.ts, tsc --build fails with a rootDir error. Give two changes that fix it, and explain which one you would pick.
Show answer
The error appears because with no include, the project covers its whole folder, so test/greet.test.ts is part of a project whose rootDir is src. Fix 1: add "include": ["src"] to packages/core/tsconfig.json. Fix 2: add "exclude": ["test"]. Pick the first, because an explicit include lists what the project owns, while exclude lists only what to skip, so a new folder would be pulled in again. Also add packages/core/test/tsconfig.json with a reference to .., so the tests are still type-checked in a project of their own.
Exercise
A teammate adds "references": [{ "path": "./packages/core/test" }] to packages/core/tsconfig.json so that the core build also checks its tests. Why does this break the design?
Show answer
It makes core depend on its own tests, while the tests already depend on core. That is a cycle, and tsc --build cannot find a valid build order. It also defeats the purpose of the separation, since test code would become part of the library's dependency graph. The root solution file should list the test project instead.