Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 987
David Norman Avatar asked Oct 02 '13 16:10

David Norman


2 Answers

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

like image 132
Ryan Cavanaugh Avatar answered Oct 06 '22 13:10

Ryan Cavanaugh


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
like image 22
profimedica Avatar answered Oct 06 '22 14:10

profimedica