What is ts-mocha?
The ts-mocha package is a TypeScript test runner for Mocha, allowing you to write and run Mocha tests in TypeScript without needing to compile your TypeScript files beforehand. It simplifies the process of testing TypeScript code by integrating directly with Mocha.
What are ts-mocha's main functionalities?
Running TypeScript Tests
This feature allows you to run Mocha tests written in TypeScript directly. The code sample demonstrates a simple test case using Mocha's `describe` and `it` functions to test an array's `indexOf` method.
const assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.strictEqual([1, 2, 3].indexOf(4), -1);
});
});
});
Integration with TypeScript
This feature highlights the ability to use TypeScript-specific features such as type annotations within your tests. The code sample shows a test case using Chai's `expect` assertion library to validate a TypeScript variable.
import { expect } from 'chai';
describe('TypeScript Test', () => {
it('should support TypeScript features', () => {
const result: number = 5;
expect(result).to.equal(5);
});
});
Configuration via tsconfig.json
This feature allows you to configure TypeScript compilation settings using a `tsconfig.json` file. The code sample shows a basic `tsconfig.json` configuration that specifies compiler options and includes/excludes certain files.
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "./dist",
"rootDir": "./src",
"strict": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
Other packages similar to ts-mocha
mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. While Mocha itself does not support TypeScript out of the box, it can be used with TypeScript by using additional tools like ts-node or by pre-compiling TypeScript files.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using Babel, TypeScript, Node.js, React, Angular, Vue.js, and Svelte. Jest has built-in support for TypeScript, making it a popular choice for testing TypeScript projects.
ava
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation. It supports TypeScript through the use of additional configuration and tools like ts-node, similar to how ts-mocha operates.
TS-Mocha

ts-mocha
is a streamlined process wrapper for mocha that simplifies running tests written in TypeScript. It automatically handles the necessary configuration and adds custom arguments, saving you from the hassle of managing a complex test setup in your project.
Why?
Setting up Mocha to work with TypeScript can be challenging and time-consuming. It requires careful configuration to ensure compatibility between the two tools. Additionally, breaking changes in mocha or ts-node can disrupt your setup, forcing you to spend valuable time troubleshooting and fixing it.
ts-mocha
eliminates these headaches by handling all the configuration for you. It allows you to run tests in TypeScript just as easily as you would with regular Mocha and JavaScript, without worrying about the underlying setup.
On top of that, we’ve included several useful options tailored specifically for TypeScript projects to make your development experience even smoother. Check them out below!
Installation
npm i -D ts-mocha
npm i -D mocha ts-node tsconfig-paths
npm i -D @types/mocha @types/expect
npm i -D tsconfig-paths
Usage
- CLI Usage
CLI options consist of all the options of regular Mocha plus extra options below:
-p, --project <value>
- relative or absolute path to a tsconfig.json
file (equivalent of tsc -p <value>
) [default: "./tsconfig.json"]
Example:
ts-mocha -p src/tsconfig.json src/**/*.spec.ts
--paths
- opt-in feature toggle flag to enable tsconfig-paths
integration [default: false]
For this to work you need to have tsconfig-paths
package installed in your project
Example:
ts-mocha --paths -p src/ src/**/*.spec.ts
This feature is required if you're using path mapping feature in your project (paths
compiler option in tsconfig.json
). When enabled with this package it allows for correct module resolution of aliased modules during TypeScript code execution within NodeJS environment.
Check our test suite for a reference implementation: Link
--type-check
- feature toggle flag to enable type checking in ts-node [default: false]
By default ts-mocha
use the --transpile-only
option of ts-node to make tests run significantly faster. If you want to run your tests slower but with type-checking you can use the --type-check
option to enable that.
Example:
ts-mocha --type-check -p src/ src/**/*.spec.ts
Watch Mode
If you want your tests to be automatically rerun when your code changes, add both the -w
flag and the --watch-files
flag telling it to watch for specified TypeScript files.
Example:
ts-mocha test/**/*.spec.ts -w --watch-files '**/*.ts'
- Programmatic usage
In code you can use ts-mocha by adding a single require at the beginning of your script:
process.env.TS_NODE_PROJECT = "./src/tsconfig.json";
process.env.TS_CONFIG_PATHS = true;
require("ts-mocha");
For example:
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);
});
});
How it works?
ts-mocha
relies on a single dependency: ts-node. This TypeScript runtime enables the execution of tests that can import and run TypeScript source files directly.
As a lightweight wrapper, ts-mocha launches a Node process with Mocha and configures the ts-node environment to handle .ts and .tsx files seamlessly. To optimize performance, type-checking is disabled by default, and ts-node operates in transpile-only mode for faster test execution.
NOTE: This package does not include Mocha - Mocha is set as peer dependency, so I don't lock the consumer to a specific Mocha version and I don't have to update this package when Mocha is updated.