Sub topic 13
Separate Projects for Tests and Tooling
Assume you know basic TypeScript types, tsconfig.json, ES modules, and the tsc command. This chapter focuses on keeping test code and repository tools separate from the production projects they use.
A test suite and a build tool are both TypeScript programs, but they usually have different:
- Inputs and output directories
- Runtime libraries and type packages
- Build commands
- Dependencies
- Compiler settings
Putting all of them in one project makes the project boundary unclear. A test-only import can accidentally become part of a production build. A tool's Node.js types can also hide browser or library type errors.
A test project can reference production projects
Suppose the repository has core and app projects. Create a third project for tests:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist-tests",
"types": ["node", "vitest/globals"]
},
"include": ["src"],
"references": [
{ "path": "../../packages/core" },
{ "path": "../../packages/app" }
]
}The test project is composite because it participates in the build graph. Its references entries declare that core and app must be built first.
A test file can then import the application through the project boundary:
import { greet } from "../../packages/core/src/greet.js";
const result = greet("test");
if (result !== "Hello, test!") {
throw new Error(`Unexpected result: ${result}`);
}
console.log("test passed");The compiler can use the referenced project's declaration output rather than checking every source file again. Your test runner may execute TypeScript through its own transformer, or it may execute JavaScript emitted by the test project. Those are separate runtime choices. tsc --build checks the types and produces the configured output; it does not replace a test runner.
Tooling deserves its own project
Repository tools include code generators, release scripts, migration helpers, and configuration loaders. They usually run in Node.js and should not be included in a browser or library project.
For example, a tooling project might use:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist-tools",
"module": "NodeNext",
"types": ["node"]
},
"include": ["src"],
"references": [
{ "path": "../../packages/core" }
]
}This tool can reuse core, but it does not need to reference app unless it actually imports application code. Keep references minimal. A reference is both a build dependency and a statement about architectural direction.
A separate project also prevents configuration conflicts. For example, a tool may need Node.js APIs and file-system access, while a browser package should not expose those types.
Add tests and tools to the solution
The root solution config can include both projects:
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" },
{ "path": "./packages/tests" },
{ "path": "./tools" }
]
}tsc --build follows the complete dependency graph. It builds core before app, then builds tests and tools when their dependencies are ready. Test projects can be excluded from a production CI command by using a production-only solution file, while a full validation command builds every project.
Exercise
Why should a test project reference an application project instead of adding the application's source files to its own include list?
Show answer
A project reference preserves the boundary, uses the application's declarations, gives the build graph a dependency to follow, and prevents the test project from silently compiling the application's source as its own files.