Write tests for TypeScript projects with mocha and chai — in TypeScript!

UPDATE: There’s a newer version of this post, with more details and solutions. It’s advisable to head there instead. The contents of this…

UPDATE: There’s a newer version of this post, with more details and solutions. It’s advisable to head there instead. The contents of this post may be outdated and the solutions posted here may no longer work.

TypeScript is something I’m crazy about, and I recently started a project in TypeScript which needed tests. In this post, I explain how to set up a simple setup where we can have our tests also written in TypeScript, wherein we can use our TypeScript import syntax, and have type inference helping with whatever we do.

We have a lot of tools and frameworks for JavaScript testing, like mocha, jasmine, and whatnot. These are mainly BDD/TDD test runners. They can make use node’s own assert library, or other external assertion libraries like chai. In my current implementation I’ve chosen mocha and chai — simply because they were easy to get up and running with in TypeScript the way I wanted. I did try Jasmine but I wasn’t able to get it working the way I wanted — mostly because I lacked the know-how. If someone could help with a Jasmine setup, that would be awesome.

NOTE: The following is cross platform, so wherever you see the $ symbol in code, remember to use your terminal, which might be Terminal on a Mac, Shell in Ubuntu/Linux, and CommandPrompt/PowerShell on Windows.

Installing the development dependencies

We are going to use mocha as our testing framework or test runner, and chai as our assertion library. We will be using another package called ts-node to help us write our tests in TypeScript. We will be installing these globally, and also as development dependencies:

Installing the type definitions

Before writing our tests in TypeScript, it’s best to have those pesky type annotations for our testing and assertion libraries/frameworks, right? Let’s use typings for this, although you might already be using it in case you’re already developing with TypeScript. It is a type definition manager which helps with TypeScript’s type assertions. In case we don’t have it installed, let’s install it now:

Now that we have typings installed, let’s get setup with the required type definitions. We will be including mocha as a global module (we can use it without import statements), and chai as a dependency (we need to import it in our tests). We will be installing type definitions for mocha from DefinitelyTyped, a repository for type definitions, while we will make use of the npm registry itself for chai:

EDIT: There is now another method to add type definitions, from TypeScript 2.0, so we can also use that instead of the typings method mentioned above. This method does not depend upon the typings npm package or module. To make use of this, type the following commands:

Writing our first TypeScript test

For the sake of explaining a test, let me write some basic hello word function. I just need a function to test, so I’m just going to create a function that returns “Hello World!” as a string when called.

Let’s write a test in TypeScript, now that we have a function to test:

Running our tests

Let’s use npm test to run our tests. We just need to hook it up to mocha and ts-node. Add the following script to your package.json:

We just registered ts-node with mocha, and the last parameter specifies that mocha should run tests in the src folder with the name test.ts. Feel free to modify this to suit your projects.

Here’s a sample test result:

What do you think?

Did this work for you? Does having type definitions in your tests help?
Could I have done something better?
Have I missed something?

Please share your thoughts using comments on this post. Also let me know if there are particular things that you would enjoy reading further.


If you enjoyed reading, please don’t forget to recommend and share — thanks!

Subscribe to The ArtfulDev Journal

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.