Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
npx @tessl/cli install tessl/npm-ts-mocha@11.1.0TS-Mocha is a lightweight wrapper around Mocha that simplifies running TypeScript tests with ts-node runtime. It eliminates the complexity of manually configuring TypeScript test environments by automatically handling ts-node registration and providing TypeScript-specific options for path mapping, type checking, and project configuration.
npm install -D ts-mocha mocha ts-nodeFor programmatic usage:
// Set configuration via environment variables before requiring
process.env.TS_NODE_PROJECT = "./tsconfig.json";
process.env.TS_CONFIG_PATHS = true; // optional
require("ts-mocha");# Basic usage - runs TypeScript tests
ts-mocha test/**/*.spec.ts
# With specific tsconfig
ts-mocha -p src/tsconfig.json src/**/*.spec.ts
# With path mapping support
ts-mocha --paths -p src/tsconfig.json src/**/*.spec.ts
# With type checking enabled
ts-mocha --type-check test/**/*.spec.ts
# Watch mode
ts-mocha test/**/*.spec.ts -w --watch-files '**/*.ts'// Basic programmatic usage
process.env.TS_NODE_PROJECT = "./src/tsconfig.json";
require("ts-mocha");
const Mocha = require("mocha");
const mocha = new Mocha();
mocha.addFile("./src/file.spec.ts");
mocha.run((failures) => {
process.on("exit", () => {
process.exit(failures);
});
});TS-Mocha is built around a simple yet effective two-layer architecture:
bin/ts-mocha): Command-line interface that parses TypeScript-specific arguments (-p, --paths, --type-check) and sets corresponding environment variables, then spawns Mocha with the registration module preloadedsrc/index.js): Handles ts-node registration and tsconfig-paths integration based on environment variables, executed before Mocha runs user testsCommand-line wrapper that spawns Mocha with TypeScript support.
ts-mocha [mocha-options] [files...]
# TypeScript-specific options:
# -p, --project <path> Path to tsconfig.json (default: ./tsconfig.json)
# --paths Enable tsconfig-paths integration
# --type-check Enable TypeScript type checkingModule registration for TypeScript transpilation in Node.js environment.
// Configuration via environment variables
process.env.TS_NODE_PROJECT = string; // Path to tsconfig.json
process.env.TS_CONFIG_PATHS = any; // Enable tsconfig-paths when truthy
process.env.TS_TYPE_CHECK = any; // Enable type checking when truthy
// Registration (side-effect only)
require("ts-mocha");Environment variables that control ts-node behavior.
// Primary configuration variables
TS_NODE_PROJECT: string; // TypeScript project configuration file path
TS_CONFIG_PATHS: any; // Enable tsconfig-paths integration (truthy)
TS_TYPE_CHECK: any; // Enable type checking instead of transpile-only (truthy)
// Deprecated
_TS_PROJECT_PATH__: string; // Deprecated alias for TS_NODE_PROJECTThe package reads TypeScript configuration from the specified tsconfig.json file:
./tsconfig.json-p or --project flagTS_NODE_PROJECT environment variableEnable TypeScript path mapping resolution:
--pathsTS_CONFIG_PATHS=trueControl TypeScript type checking behavior:
--type-check to enable type checkingTS_TYPE_CHECK=trueThe package handles ts-node registration errors:
// Error handling pattern (internal)
try {
const project = process.env.TS_NODE_PROJECT ||
process.env._TS_PROJECT_PATH__ ||
'./tsconfig.json';
const transpileOnly = !process.env.TS_TYPE_CHECK;
require('ts-node').register({
project,
transpileOnly,
});
if (process.env.TS_CONFIG_PATHS) {
require('tsconfig-paths/register');
}
} catch (error) {
console.log('[ERROR] ' + error.message);
process.exit(1);
}Common error scenarios:
// Full programmatic setup
process.env.TS_NODE_PROJECT = "./test/tsconfig.json";
process.env.TS_CONFIG_PATHS = true;
require("ts-mocha");
const Mocha = require("mocha");
const path = require("path");
const mocha = new Mocha();
mocha.addFile(path.resolve(__dirname, "app.spec.ts"));
mocha.run((failures) => {
process.on("exit", () => {
process.exit(failures);
});
});{
"scripts": {
"test": "ts-mocha test/**/*.spec.ts",
"test:watch": "ts-mocha test/**/*.spec.ts -w --watch-files '**/*.ts'",
"test:typecheck": "ts-mocha --type-check test/**/*.spec.ts"
}
}# Different configs for different test suites
ts-mocha -p test/unit/tsconfig.json test/unit/**/*.spec.ts
ts-mocha -p test/integration/tsconfig.json test/integration/**/*.spec.ts