ciplogic.com
Live with grace. Write superb software.

I am writing some unit tests for my TypeScript collection classes (actually migrating some older tests), and I decided in going with Mocha.

How easy it is? Well, as simple as 1, 2, 3 ... 4 :) :

1. Install the typing files

tsd install mocha
tsd install node
tsd install assert

2. Write your test(s)

For this example, I'm presenting you a super simple test, but obviously nothing stops you from adding new ones (check my github core-lang project for details):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <reference path="../../../typings/mocha/mocha.d.ts"/>
/// <reference path="../../../typings/node/node.d.ts"/>
/// <reference path="../../../typings/assert/assert.d.ts"/>
 
import { list, XIterable, XIterator } from "../../main/core/Iterable";
import assert = require("assert");
 
describe('ArrayList', function() {
    it('should allow filtering', function() {
        var items : XIterable<number> = list([1, 2, 3, 4]),
            filteredItems: XIterable<number>,
            iterator : XIterator<number>;
 
        filteredItems = items.filter(it => it % 2 == 0);
        iterator = filteredItems.iterator();
        
        assert.equal(iterator.next(), 2);
        assert.equal(iterator.next(), 4);
        assert.notEqual(iterator.hasNext(), true);
    });
});

3. Build your test(s)

I use Grunt, so I have (I deleted all the non test stuff), but is basically easy to see the target that is named "test" across all the plugins in the original full Grunt file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Grunt project configuration.
 */
module.exports = function(grunt) {
    // configuration for the plugins.
    grunt.initConfig({
        clean: {
            test : [
                "target/test"
            ]
        },
 
        typescript: {
            "test" : {
                options: {
                    module : "commonjs",
                    sourceMap: true,
                    declaration: true,
                },
                files: [{
                    dest: "target/test/",
                    src: [
                        "src/test/core/**/*.ts",
                        "src/test/core/**/*.d.ts"
                    ]
                }]
            }
        },
 
        mochaTest: {
            test: {
                options: {
                    reporter: "spec",
                    captureFile: "target/test/tests_results.txt"
                },
                src: ["target/test/**/*.js"]
            }
        }
    });
 
    // load NPM tasks:
    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks("grunt-typescript");
    grunt.loadNpmTasks("grunt-mocha-test");
 
    // register our tasks:
    grunt.registerTask("test", ["clean:test",
                                "typescript:test",
                                "mochaTest:test"]);
 
    grunt.registerTask("default", ["test"]);
};

Yes, that is all the configuration that you need to do full builds, including running the test suite. Sweet!

I use a layout similar with maven (src/main holds the main sources, src/test holds the test sources, stuff that can get discarded gets built into the target folder).

4. Run them

$ grunt test
Running "clean:test" (clean) task
>> 1 path cleaned.
 
Running "typescript:test" (typescript) task
6 files created. js: 2 files, map: 2 files, declaration: 2 files (1629ms)
 
Running "mochaTest:test" (mochaTest) task
 
 
  ArrayList
    ✓ should allow filtering
 
 
  1 passing (7ms)
 
 
Done, without errors.

And because we're writing TypeScript, we get all the cool-cool stuff such as autocomplete, or jump to definition, etc. using Visual Studio Code.

Man, I really-really like TypeScript.

Disqus Comments

comments powered by Disqus

Germanium

The one to rule them all. The browsers that is.

SharpKnight

SharpKnight is an Android chess game.

MagicGroup

MagicGroup is an eclipse plugin.