Registry / testing / ts-node-test-register

ts-node-test-register

JSON →
library10.0.0jsnpmunverified

ts-node-test-register is a utility package designed to simplify running TypeScript tests by integrating with test runners like Mocha. It enables the use of a dedicated `tsconfig.json` file (e.g., `test/tsconfig.json`) specifically for test code, allowing for different compiler options (like `allowJs`) compared to the main project's configuration. The package ensures type checking is active by default during testing, adhering to the principle that test code should also be type-safe. It closely tracks major releases of its peer dependency, `ts-node`, with the current stable version being 10.0.0. Its main differentiator is the automatic loading of test-specific `tsconfig.json` files and explicit enabling of type checks, providing a robust environment for TypeScript testing.

npm install ts-node-test-register
INSTALL
IMPORT
SIG · TS-NODE-TEST-REGIS
T
ts-node-test-register
testingjavascriptv10.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

(side-effect CommonJS)
require('ts-node-test-register');
import { register } from 'ts-node-test-register';
This package is primarily designed to be loaded for its side effects in a CommonJS environment, typically via a `--require` flag in test runners. It does not export any named symbols.
(side-effect ESM)
import 'ts-node-test-register';
const register = require('ts-node-test-register');
For ESM projects, a side-effect import can be used. However, direct programmatic import is less common than using the `--require` CLI flag or configuring through a test runner's configuration file.
(CLI `--require` flag)
mocha --require ts-node-test-register "test/**/*.ts"
mocha --require './node_modules/ts-node-test-register/index.js' "test/**/*.ts"
The most common and recommended usage pattern is to pass `ts-node-test-register` directly as an argument to the test runner's `--require` (or similar) flag, letting Node.js resolve the module path.

Demonstrates how to configure Mocha to use `ts-node-test-register` to run TypeScript tests, loading a dedicated `test/tsconfig.json` for test-specific compiler options.

{ "name": "my-typescript-project", "version": "1.0.0", "description": "Example project using ts-node-test-register with Mocha", "main": "src/index.ts", "scripts": { "test": "mocha --require ts-node-test-register \"test/**/*.ts\"" }, "devDependencies": { "mocha": "^10.0.0", "ts-node": "^10.0.0", "typescript": "^5.0.0", "ts-node-test-register": "^10.0.0" } } // test/tsconfig.json // This config allows ts-node to compile test files with specific settings. { "compilerOptions": { "module": "CommonJS", "target": "ES2018", "lib": ["ESNext", "DOM"], "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "allowJs": true, // Example: enables JavaScript files in tests "rootDir": "./" }, "include": ["../src/**/*.ts", "./**/*.ts"], "exclude": ["node_modules"] } // src/index.ts export function add(a: number, b: number): number { return a + b; } export function subtract(a: number, b: number): number { return a - b; } // test/example.test.ts import { add, subtract } from '../src/index'; import assert from 'assert'; describe('Arithmetic functions', () => { it('add() should return the sum of two numbers', () => { assert.strictEqual(add(1, 2), 3); assert.strictEqual(add(-1, 5), 4); }); it('subtract() should return the difference of two numbers', () => { assert.strictEqual(subtract(5, 2), 3); assert.strictEqual(subtract(1, 10), -9); }); });
Debug
Known issues
breakingMajor versions of `ts-node-test-register` (e.g., v8, v9, v10) are tightly coupled to major versions of its peer dependency, `ts-node`. Upgrading `ts-node-test-register` will automatically update the required `ts-node` version in `peerDependencies`, potentially introducing breaking changes inherited directly from `ts-node` itself.
fix
Always review the release notes for both `ts-node-test-register` and the corresponding major version of `ts-node` when upgrading. Ensure your installed `ts-node` version matches the peer dependency range specified by `ts-node-test-register`.
affects: >=2.0.0
gotcha`ts-node` is listed as a peer dependency, meaning it must be explicitly installed in your project alongside `ts-node-test-register`. Failing to install `ts-node` will result in runtime errors as the register module depends on its presence for TypeScript compilation.
fix
Install `ts-node` as a development dependency: `npm install ts-node --save-dev` or `yarn add ts-node --dev`.
affects: >=1.0.0
gotchaThis package enforces type checking by default for test files. While beneficial for ensuring high-quality test code, this can lead to slower test execution or failures if test files contain type errors that might otherwise be ignored in a 'transpile-only' `ts-node` configuration.
fix
Ensure your test files are free of TypeScript errors. If specific type behaviors are required for tests, configure them within your dedicated `test/tsconfig.json` to reflect the desired type-checking strictness.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ts-node' from '<path-to-project>'
The `ts-node` peer dependency is missing from your project's `node_modules`. `ts-node-test-register` relies on `ts-node` for its core functionality.
fix
Install `ts-node` as a development dependency: `npm install ts-node --save-dev` or `yarn add ts-node --dev`.
TypeError: Unknown file extension ".ts" for .../test/example.test.ts
The test runner (e.g., Mocha) is not correctly configured to use `ts-node-test-register` to handle TypeScript files, or `ts-node-test-register` itself isn't being loaded before test execution.
fix
Ensure you are passing `--require ts-node-test-register` to your test command in `package.json` scripts or configuring it in your test runner's configuration file (e.g., `.mocharc.json`). Double-check the path if manually specifying it.
TSxxxx: [your TypeScript error message here]
TypeScript errors are reported during test execution. `ts-node-test-register` enables full type checking, so any type violations in your test code or the source files it imports will halt the process.
fix
Address the specific TypeScript error reported. This might involve correcting type definitions, adjusting compiler options in `test/tsconfig.json`, or ensuring all dependencies are correctly typed and installed.
Upgrade
Version history
10.0.0latest on npm
Audit
Dependencies
ts-noderequiredRuntime peer dependency required for transpiling TypeScript files during test execution.
Agent activity
15 hits · last 30 days
node
14
Resources
ts-node-test-register — npm install ts-node-test-register · libregistry