Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
Installations
npm install ts-mocha
Developer
piotrwitek
Developer Guide
Module System
CommonJS
Min. Node Version
>= 6.X.X
Typescript Support
No
Node Version
16.14.2
NPM Version
8.5.0
Statistics
190 Stars
96 Commits
24 Forks
6 Watching
3 Branches
13 Contributors
Updated on 09 Nov 2024
Bundle Size
46.67 kB
Minified
15.35 kB
Minified + Gzipped
Languages
JavaScript (66.71%)
TypeScript (33.29%)
Total Downloads
Cumulative downloads
Total Downloads
50,978,935
Last day
-26.6%
83,953
Compared to previous day
Last week
0.9%
533,393
Compared to previous week
Last month
35.9%
1,976,099
Compared to previous month
Last year
-3.4%
13,281,354
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Peer Dependencies
1
Dev Dependencies
8
Optional Dependencies
1
TS-Mocha
ts-mocha
is a wrapper on top ofmocha
to allow running tests written in TypeScript without setting up a complicated setup in your project, it just works.
All
mocha
features are available without any limitation becausets-mocha
is passing all the params to originalmocha
behind the scenes.
Why?
To setup Mocha with TypeScript you need to figure out how to set it up together, it's not an easy task and require some time and effort to setup correctly. Moreover this setup will stop working whenever mocha
or ts-node
introduce breaking changes, so you'll have to fix it and waste your time again.
This package handles all that concerns for you and let you use ts-mocha
in the same way as a regular mocha while supporting TypeScript.
Also we added some useful options to make your life easier specifically for TypeScript projects, you can find them below.
How?
TS-Mocha has one only dependency - ts-node, which is used as a TypeScript runtime to execute tests that can import and run imported TypeScript source files as well. It is a thin wrapper that run node process with mocha and set up ts-node environment to handle .ts
and .tsx
files. To speed up TypeScript tests execution type-checking is disabled, using only transpile mode.
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.
PRO TIP: To make your developer experience better I recommend to run type-checking in a separate process by starting TSC compiler (preferably in watch mode) in you terminal with --noEmit and --project flags.
Installation
1# remember to install mocha if you don't have it already (npm i -D mocha) 2 3npm i -D ts-mocha 4 5# install recent Mocha and Expect @types packages for best DX 6npm i -D @types/mocha @types/expect
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:
1ts-mocha -p src/tsconfig.json src/**/*.spec.ts
--paths
- feature toggle flag to enable tsconfig-paths
integration [default: false]
tsconfig-paths
is an optional dependency, make sure to install it locally in your project
When using path mapping via the paths
compiler option in tsconfig.json
this library utilizes the tsconfig-paths
package, allowing for automatic resolution of aliased modules locations during test execution.
Check our test suite for a reference implementation: Link
Example:
1ts-mocha --paths -p src/ src/**/*.spec.ts
--type-check
- feature toggle flag to enable type checking in ts-node [default: false]
By default ts-mocha uses the --transpile-only
option of ts-node to make tests run faster. Use the --type-check
option to enable type checking in ts-node.
Example:
1ts-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 typescript files.
Example:
1ts-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:
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});
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 9/25 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/piotrwitek/ts-mocha/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/piotrwitek/ts-mocha/ci.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:27
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
9 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2024-11-18
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