Thomas
Thomas

Reputation: 182048

How to run Mocha tests written in TypeScript?

Setup: I have a Node project (pure Node, no browser bits) written in TypeScript. I can use the TypeScript compiler (tsc) from the typescript module to compile the code. So far so good.

However, I want to write tests using Mocha, and that's where I'm having trouble. I tried --compilers ts:typescript, but I keep getting errors like:

error TS5023: Unknown compiler option 'compilers'.

It looks like the command line to mocha ends up being passed to tsc, which is obviously not good.

Upvotes: 71

Views: 57025

Answers (5)

Shahrad Elahi
Shahrad Elahi

Reputation: 1352

There is a newer solution for using mocha with TypeScript, and it's using tsx node loader.

First, install the tsx package with the following command:

npm install --save-dev tsx

Then, create a .mocharc.json file with the following content:

{
  "$schema": "https://json.schemastore.org/mocharc.json",
  "require": "tsx"
}

Now you're ready to use mocha with TypeScript.

References

Upvotes: 28

Matt Jensen
Matt Jensen

Reputation: 1564

Configure ts-node as your loader.

Add .mocharc.js to project root:

module.exports = {
  loader: 'ts-node/esm',
};

Ensure you have ts-node installed: npm install ts-node -D. Source here.

Upvotes: 2

jpierson
jpierson

Reputation: 17384

For anybody who has tried and had problems with typescript-require you may want to try ts-node.

$ npm install -g ts-node
$ mocha --require ts-node/register src/**/*.spec.ts

It also appears that there has been some ongoing discussion about deprecating typescript-require in favor of ts-node.

Upvotes: 107

ToDevAndBeyond
ToDevAndBeyond

Reputation: 1503

Using the latest version of Mocha and ts-node I was getting an Unexpected token import issue. Using the below settings with ts-mocha worked for me:

tsconfig.json

{
    "files": [
        "src/main.ts"
    ],
    "compilerOptions": {
        "noImplicitAny": true,
        "target": "es2015",
        "types": ["mocha"],
        "module": "commonjs"
    }
}

package.json

"scripts": {
    "mocha": "ts-mocha -p library/tsconfig.json library/test/**/*.ts"
  },

launch.json

{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "runtimeArgs": [
        "${workspaceFolder}/node_modules/ts-mocha/bin/ts-mocha",
        "--timeout", "999999",
        "-p",
        "${workspaceFolder}/library/tsconfig.json",
        "${workspaceFolder}/library/test/**/*.ts"
    ],
    "internalConsoleOptions": "openOnSessionStart"
}

and gulp.js just incase you want to use gulp too

const gulp = require('gulp');
const ts = require('gulp-typescript');
const mocha = require('gulp-mocha');

const tsProject = ts.createProject('tsconfig.json');

gulp.task('build', () => tsProject.src()
  .pipe(tsProject())
  .js.pipe(gulp.dest('dist')));

gulp.task('test', () => gulp.src('test/*.spec.ts')
  .pipe(mocha({
    reporter: 'nyan',
    require: ['ts-node/register'],
  })));
/* single command to hook into VS Code */
gulp.task('default', gulp.series('build', 'test'));

Upvotes: 6

Thomas
Thomas

Reputation: 182048

Don't use this answer. typescript-require is unmaintained, and ts-node is its replacement. Leaving this answer here for posterity.

Found it. The typescript module is actually like a "main" function; it runs the compiler as soon as the module is loaded. Not very nice design.

I poked at Mocha's acceptance tests, which show how to use a custom compiler for foo files. They wire it up via the require.extensions mechanism. I was halfway through writing a module that just calls tsc on the command line when I realized that somebody must have done this before. So it's very simple:

$ npm install typescript-require --save-dev
$ mocha --compilers ts:typescript-require

Upvotes: 10

Related Questions