David Norman David Norman - 1 year ago 274
TypeScript Question

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

I'm trying to get TypeScript, mocha, and chai working together when running on the command line. I'm using TypeScript version

I have CalculatorTest.ts:

/// <reference path="../definitions/mocha.d.ts" />
/// <reference path="../definitions/chai.d.ts" />

// import chai = require('node_modules/chai/chai');

var expect = chai.expect;

describe("Calculator", () => {
var calc: Calculator;

beforeEach(() => {
calc = new Calculator();

describe("Add", () => {
it("should have correct results", () => {



it("this test should fail", () => {

I also have a separate Calculator.js file.

I can run this fine in the browser with the page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Mocha Calculator Tests</title>
<link rel="stylesheet" href="scripts/node_modules/mocha/mocha.css" />
<div id="mocha"></div>
<script src="scripts/node_modules/mocha/mocha.js"></script>
<script src="scripts/node_modules/chai/chai.js"></script>
<script src="scripts/Calculator.js"></script>
<script src="scripts/test/CalculatorTest.js"></script>

However, if I try to run on the command line with



./node_modules/mocha/bin/mocha -r chai

I get the error:

var expect = chai.expect;
ReferenceError: chai is not defined
at Object.<anonymous> (C:\javascript\Test\Test\scripts\test\CalculatorTest.js:4:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)

Is there some way I should look up the chai object?

Update: Here's more information on what happens when the import is uncommented:

If I change the chai.d.ts from DefinitelyTyped so that it starts

declare module 'chai' {

and change the top few lines in the file to:

/// <reference path="../definitions/mocha.d.ts" />

import chai = require('../definitions/chai');

var expect = chai.expect;

then the file compiles successfully.

However, when I run from the command line, I get

% ./node_modules/mocha/bin/mocha

define(["require", "exports", '../definitions/chai'], function(require, export
ReferenceError: define is not defined
at Object.<anonymous> (C:\javascript\Test\Test\scripts\test\CalculatorTest.js:2:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)

The generated JavaScript file starts with:

/// <reference path="../definitions/mocha.d.ts" />
define(["require", "exports", '../definitions/chai'], function(require, exports, __chai__) {
var chai = __chai__;

I believe that TypeScript is generating a module here, since I have the import statement.

Answer Source

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 {


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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download