Running a mocha test using Chai and TypeScript from the command line

I'm assuming you're using the chai.d.ts from DefinitelyTyped.

Since you're using chai as an external module (through imports), you'll need to modify the .d.ts file. Change

declare module chai {

to

declare module 'chai' {

Then you can write this and everything should just work:

import chai = require('chai');

If you want to use this in a webpage, you'll have to use RequireJS and compile differently for node (--module commonjs) than for the web (--module amd).


Take my example for a complete setup to run typescript tests with mocha and chai:

test.ts

import { expect } from 'chai';
import 'ts-node';
import 'mocha';

describe('#MyDummyTest', () => {
    it('sould convert be true!', () => {
        const result = true;
        expect(result).to.equal(true);
    });
});

packaje.json

"devDependencies": {
    "@angular/cli": "^1.4.9",
    "@types/chai": "^4.0.4",
    "@types/mocha": "^2.2.44",
    "chai": "^4.1.2",
    "mocha": "^3.5.3",
    "ts-node": "^3.3.0",
    "typings": "^2.1.1"
},
"scripts": {
  "test": "mocha --compilers ts:ts-node/register,tsx:ts-node/register --reporter spec test/test.ts"
  "test-w": "mocha --compilers ts:ts-node/register,tsx:ts-node/register --watch --reporter spec test/test.ts"
}

tsconfig.json

"compilerOptions": {
    "types": [ "mocha", "chai" ],
    "typeRoots": [
        "./node_modules/@types"
    ]
}

This list might help you too:

npm install -g ts-node
npm install -g typescript

Install type definitions:

npm install typings -g
npm install typings --save-dev

Install type definitions:

npm install @types/chai
npm install @types/mocha --save-dev

Install type definitions:

typings install dt~mocha --global --save
typings install npm~chai --save

Install reporter:

npm install ts-node --save-dev

Install ts mocha:

npm i -g ts-mocha