Gathering detailed insights and metrics for ts-mocha
Gathering detailed insights and metrics for ts-mocha
Gathering detailed insights and metrics for ts-mocha
Gathering detailed insights and metrics for ts-mocha
Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
npm install ts-mocha
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (68.6%)
TypeScript (31.4%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
194 Stars
109 Commits
26 Forks
5 Watchers
3 Branches
15 Contributors
Updated on Jun 15, 2025
Latest Version
11.1.0
Package Id
ts-mocha@11.1.0
Unpacked Size
10.05 kB
Size
4.31 kB
File Count
5
NPM Version
10.2.4
Node Version
20.11.1
Published on
Feb 20, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
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.
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!
1# remember to install mocha if you don't have it already (npm i -D mocha) 2 3npm i -D ts-mocha 4 5# and also required peerDependencies 6npm i -D mocha ts-node tsconfig-paths 7 8# also relevant @types packages for best DX: Mocha and Expect 9npm i -D @types/mocha @types/expect 10 11# there is also an optional peerDependency if you're using it in your project 12npm i -D tsconfig-paths
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:
1ts-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:
1ts-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:
1ts-mocha --type-check -p src/ src/**/*.spec.ts
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:
1ts-mocha test/**/*.spec.ts -w --watch-files '**/*.ts'
In code you can use ts-mocha by adding a single require at the beginning of your script:
1// set env variable to the `tsconfig.json` path before loading mocha (default: './tsconfig.json') 2process.env.TS_NODE_PROJECT = "./src/tsconfig.json"; 3 4// Optional: set env variable to enable `tsconfig-paths` integration 5process.env.TS_CONFIG_PATHS = true; 6 7// register mocha wrapper 8require("ts-mocha");
For example:
1process.env.TS_NODE_PROJECT = "./src/tsconfig.json"; 2require("ts-mocha"); 3const Mocha = require("mocha"); 4 5const mocha = new Mocha(); 6mocha.addFile(`./src/file.spec.ts`); 7mocha.run((failures) => { 8 process.on("exit", () => { 9 process.exit(failures); // exit with non-zero status if there were failures 10 }); 11});
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 3
Details
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More