Running Tests
Quick Test Run
Run all tests in parallel (recommended):By default, hereby uses one worker per CPU core. Adjust with
--workers=<number>.Serial Test Execution
Run tests sequentially for debugging:Test Suites
TypeScript has several test suites organized by type:- Compiler Tests
- Conformance Tests
- FourSlash Tests
- Project Tests
Tests in These test compilation, emit, and error detection.
tests/cases/compiler/ verify compiler behavior:Running Specific Tests
By Name Pattern
Run tests matching a regex:By Runner
Specify which test runner to use:conformance- Conformance suitecompiler- Compiler suitefourslash- Language service suiteproject- Project suite
From Failed Tests File
Rerun previously failed tests:.failed-tests.
Test Options
- Common Options
- Advanced Options
- Reporters
Watch Mode
Automatically rerun tests when files change:You must specify
--tests or --failed to use watch mode.- Test infrastructure (
built/local/run.js) - Test cases (
tests/cases/**) - Test libraries (
tests/lib/**)
Debugging Tests
Inspector Mode
Run tests with Node.js inspector:chrome://inspect in Chrome or attach from VS Code.
VS Code Debugging
Launch Debugger
Press F5 or select Run > Start Debugging.The configuration runs Mocha with the currently open test file name.
Test Baselines
Most tests generate baseline files to detect output changes:Baseline Files
Compiler tests generate:.js- Emitted JavaScript.d.ts- Emitted declarations (in.jsfile).errors.txt- Compiler errors.types- Type of each expression.symbols- Symbol for each identifier.js.map- Source maps (if requested)
Comparing Baselines
After running tests, compare changes:Accepting Baselines
If the baseline changes are correct:tests/baselines/local/* to tests/baselines/reference/.
Writing New Tests
Adding a Compiler Test
Multi-File Tests
Use@filename tags to simulate multiple files:
Conformance Tests
For spec compliance tests, add files to appropriate subdirectories:Conformance test names must be unique across all test directories.
Test Infrastructure
The test runner is built fromsrc/testRunner/:
built/local/run.js
Test Harness
The harness code insrc/harness/ provides utilities:
- Compiler host implementations
- Virtual file systems
- Baseline management
- Test configuration parsing
ESLint Rule Tests
TypeScript includes custom ESLint rules with their own tests:Continuous Integration
Sharded Testing
CI systems can split tests across multiple machines:Coverage Reports
Generate test coverage with c8:coverage/ directory.
Troubleshooting
Tests fail with 'Cannot find module'
Tests fail with 'Cannot find module'
Build the test infrastructure:
Baseline comparison shows no differences but test fails
Baseline comparison shows no differences but test fails
Regenerate baselines:
Test times out
Test times out
Increase timeout:
Watch mode doesn't detect changes
Watch mode doesn't detect changes
Watch mode is experimental. Use manual reruns:
Debugger doesn't hit breakpoints
Debugger doesn't hit breakpoints
Ensure you’ve built tests with source maps:Source maps are enabled by default in
Herebyfile.mjs.Test Guidelines
Test Requirements
Every PR should include:
- At least one test that fails without your changes
- Reasonable permutations of the fix/feature
- Updated baselines if output changes
- Test names that clearly describe what they test
Next Steps
Debugging
Learn debugging techniques
Building
Build system reference