David Norman David Norman - 1 month ago 4
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 0.9.1.1.

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", () => {

calc.add(1);
calc.add(2);

expect(calc.current()).to.equal(3);
});

it("this test should fail", () => {
expect(calc.current()).to.equal(10000);
});
})
});


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">
<head>
<title>Mocha Calculator Tests</title>
<link rel="stylesheet" href="scripts/node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="scripts/node_modules/mocha/mocha.js"></script>
<script src="scripts/node_modules/chai/chai.js"></script>
<script>mocha.setup('bdd')</script>
<script src="scripts/Calculator.js"></script>
<script src="scripts/test/CalculatorTest.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run();
</script>
</body>
</html>


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

./node_modules/mocha/bin/mocha


or

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


I get the error:

C:\javascript\Test\Test\scripts\test\CalculatorTest.
js:4
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

C:\javascript\Test\Test\scripts\test\CalculatorTest.js:2
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

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

Comments