This document describes how tests are organized in the TypeScript compiler repository. It covers the directory structure, test categories, test configuration, and how tests are executed. For information about baseline testing specifically, see Baseline Testing. For information about Fourslash testing for language service features, see Fourslash Testing.
Tests in the TypeScript repository are organized into several categories based on their purpose and scope.
Title: Test Directory and Data Flow
Sources: Herebyfile.mjs532-551 scripts/build/tests.mjs22-23
The repository contains the following test categories:
| Category | Location | Purpose |
|---|---|---|
| Unit Tests | src/testRunner/unittests/ | Test individual functions and classes in isolation |
| Compiler Tests | tests/cases/compiler/ | Test compiler behavior and code generation |
| Conformance Tests | tests/cases/conformance/ | Verify compliance with ECMAScript and TypeScript specifications |
| FourSlash Tests | tests/cases/fourslash/ | Test language service features (completions, navigation, etc.) |
| ESLint Rule Tests | scripts/eslint/tests/ | Test custom ESLint rules |
Sources: Herebyfile.mjs553-557 package.json89-93
The test execution system is built around Mocha as the test runner, with custom infrastructure for configuration and parallel execution.
Title: Test Infrastructure Code Entities
Sources: Herebyfile.mjs679-684 scripts/build/tests.mjs34-188 package.json47-84
The primary test runner is built from the src/testRunner project and output to built/local/run.js. This file serves as the entry point for all Mocha-based tests.
Title: Test Runner Build Pipeline
Sources: Herebyfile.mjs534-551 package.json93
Tests are configured through a combination of command-line options and a dynamically generated test.config file.
The test infrastructure accepts numerous command-line options that control test execution:
| Option | Type | Purpose |
|---|---|---|
--tests=<pattern> | string | Run tests matching the specified pattern |
--failed | boolean | Run only tests listed in .failed-tests |
--light | boolean | Run tests in light mode (fewer verifications, faster) |
--dirty | boolean | Skip cleaning test output directories before running |
--workers=<count> | number | Number of parallel workers for test execution |
--timeout=<ms> | number | Override default test timeout (default: 40000ms) |
--coverage | boolean | Generate code coverage reports using c8 |
--keepFailed | boolean | Keep tests in .failed-tests even if they pass |
--shards=<count> | number | Total number of shards for distributed testing |
--shardId=<id> | number | 1-based ID of this shard |
Sources: scripts/build/options.mjs1-92 scripts/build/tests.mjs35-48
The writeTestConfigFile() function generates a test.config JSON file that passes configuration from the build system to the test runner.
Sources: scripts/build/tests.mjs209-226 scripts/build/tests.mjs87-89
The configuration file contains:
test: Array of test patterns to runlight: Boolean indicating light modeworkerCount: Number of parallel workerstimeout: Test timeout in millisecondsshards: Total number of shardsshardId: Current shard identifierTests are executed through hereby tasks that coordinate building the test infrastructure and running Mocha.
Title: Execution Workflow
Sources: scripts/build/tests.mjs34-188 Herebyfile.mjs679-684
When running tests in parallel, the system creates a temporary folder to store task configurations and divides the work among multiple workers:
/tmp/ts-tests<N>/CPU count - 1)Sources: scripts/build/tests.mjs69-81
Tests generate output files that are compared against reference baselines to detect regressions. Baseline files include:
.js: The emitted JavaScript code tests/baselines/reference/conditionalTypes1.js1-210.errors.txt: Compiler diagnostic messages tests/baselines/reference/conditionalTypes1.errors.txt1-87.types: Type information for all expressions tests/baselines/reference/conditionalTypes1.types1-272.symbols: Symbol information and definitions tests/baselines/reference/conditionalTypes1.symbols1-160Sources: scripts/build/tests.mjs22-23 scripts/build/tests.mjs190-193
Code coverage is collected using c8 (the native V8 coverage tool) and reported using monocart-coverage-reports.
Sources: scripts/build/tests.mjs147-158 package.json58 .c8rc.json1-8
Coverage configuration is defined in .c8rc.json:
src/ directory**/node_modules/**, built/**, tests/**Unit tests are organized by the feature or subsystem they test:
| Test File | Tested System | Location |
|---|---|---|
publicApi.ts | Public API contracts | src/testRunner/unittests/publicApi.ts1-328 |
tsserver/goToDefinition.ts | Language server navigation | src/testRunner/unittests/tsserver/goToDefinition.ts1-50 |
sys/symlinkWatching.ts | File system abstractions | src/testRunner/unittests/sys/symlinkWatching.ts1-50 |
Each test file uses the standard Mocha describe/it pattern.
Sources: src/testRunner/unittests/publicApi.ts9-328
The test infrastructure supports custom Mocha reporters:
.failed-tests file.Sources: package.json74 scripts/build/tests.mjs106-115
Tests can be split across multiple machines using the sharding system:
Title: CI Sharding Logic
Sources: scripts/build/tests.mjs45-46 scripts/build/tests.mjs209-226
Refresh this wiki