Server-Side Testing with Mocha, Chai and TypeScript

Introduction

As applications grow in complexity, being able to run automated tests becomes more and more important. In this post, I will go through the basics of writing and running some tests using TypeScript, along with the help of Mocha and Chai. Although this environment can be used to run on node.js and the browser, the focus today will be on the node.js scenario.

Get everything installed

First, let’s install the npm packages we need:

$ npm install mocha --save-dev
$ npm install chai --save-dev

Let’s not forget the TypeScript definition files:

$ tsd query mocha --save --action install
$ tsd query chai --save --action install

By default, Mocha looks for tests in the “test” directory, so let’s go ahead and create it:

$ mkdir test

Create some tests

Now let’s create a basic set of tests. Let’s create a TypeScript file in the test directory with the following code:

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

/**
 * Module dependencies.
 */
import chai = require('chai');

/**
 * Globals
 */

var expect = chai.expect;

/**
 * Unit tests
 */
describe('User Model Unit Tests:', () => {

    describe('2 + 4', () => {
        it('should be 6', (done) => {
            expect(2+4).to.equals(6);
            done();
        });

        it('should not be 7', (done) => {
            expect(2+4).to.not.equals(7);
            done();
        });
    });
});

Notice how we make our assertions. Chai allows for the standard TDD-style assert, but it also allows the use of BDD styles, which can be more expressive and understandable, especially to those that aren’t concerned with the specifics of the underlying implementation.

Run the tests

After we successfully compile the code, we just need to run the following command:

$ mocha

Doing so will run all the tests that Mocha can find:

mocha

Now, let’s make a test fail. We’ll grab the second test and make an expectation that 2+4 would not equal 6:

it('should not be 6', (done) => {
    expect(2+4).to.not.equals(6);
    done();
});

Running mocha again should give the following result:

mocha-fail

Conclusion

Today we were able to setup a test environment for TypeScript code, using Mocha as a test runner, along with the BDD-style syntax provided by Chai. This provides a great approach to testing code that resides in node.js applications.

Cheers!

Advertisements

Some TypeScript links of interest

I was out of town over the weekend, so this is going to be a bit of a filler-post. Nonetheless, I wanted to share some links that may be of interest if you want some more information on TypeScript and tools:

  1. Introduction to TypeScript: https://www.youtube.com/watch?v=5UPMqtrbw7E
  2. The Definitive guide to TypeScript: http://www.sitepen.com/blog/2013/12/31/definitive-guide-to-typescript/
  3. TypeScript CodePlex site: http://typescript.codeplex.com/
  4. TypeScript at Build 2014: http://channel9.msdn.com/Events/Build/2014/3-576
  5. TypeScript support in Web Essentials: http://vswebessentials.com/features/typescript

Cheers!

Automating retrieval of TypeScript definition files with TSD

Introduction

In previous posts, I have described how definition files can be retrieved via the Definitely Typed website, which leads you to their GitHub repository. This is all well and good if we need to retrieve a small number of definition files, but it can be a hassle if we want to get several definition files.

Automating definition file retrieval

Thankfully, there is a node package that helps retrieve definition files in a more automated way, So let’s go ahead and install this package, known as tsd:

$ npm install -g tsd

Querying definition files

Now that we have tsd installed, we can run queries to see if we can find definition files:

image

Installing definition files

Let’s go ahead and give it a shot with a definition file. To install the definition file for, say, mongoose, we run the command:

$ tsd query mongoose --action install

If it all goes well we should get the following:

image

By default, you will find the definition file in typings/mongoose/mongoose.d.ts.

Tracking all installed definition files

We can take this all a step further and create a tsd.json file to track what definitions we have installed. If this route is chosen, it is worth removing all previously installed definition files and proceed in the following manner:

Create the tsd.json:

$ tsd init

Opening the json file will give us a few settings:

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {} 
}

The fields of interest are as follows:

  • path: Path of where the definition files will be installed.
  • bundle: Path to a .d.ts file that contains references to all installed definition files.

To install a definition file, we can run the command:

$ tsd query mongoose --save --action install

Opening the file tsd.json we get the following:

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed":
  {
    "mongoose/mongoose.d.ts": {
      "commit": "e6a11b3cf228eacc13006558e6c9903ac7b4975b"
    }
  }
}

Looking at the file tsd.d.ts, we can see references to all definition files:

/// <reference path="node/node.d.ts" />
/// <reference path="mongoose/mongoose.d.ts" />

We can update all installed definition files with the command:

$ tsd update -so

image

Conclusion

We have successfully been able to automate the retrieval of definition files. We can query, install and update definition files for use in TypeScript projects.

Cheers!