DEV Community

Daniel Werner
Daniel Werner

Posted on • Originally published at 42coders.com on

Testing TypeScript with Mocha and Chai

Introduction

I am a huge fan of TDD, already wrote a couple of articles in this topic. In one of my recent projects I used Angular as a fronted framework. Usually I use Jasmine for testing in JavaScript (you can find the related article here). Jasmine should also work with TypeScript, but at that time, I got some issues while setting it up, so I looked for another testing framework to work with.

I found Mocha as a good solution used together with Chai assertion library. Let’s see how to set up the testing environment for TypeScript.

Installation

As a fist step we install the required packages with npm: mocha , chai , ts-node , and type definitions for both libraries:

npm install chai mocha ts-node @types/chai @types/mocha --save-dev

Create the first test

Create a simple function for testing:

export const helloTest(){ return true; }

Let’s create our first test case, and assert that out function works as expected. Obviously in real TDD we should write the test first and the function afterwards, but for demonstration purposes it should be ok.

import { helloTest } from '../src/hello-test';
import { expect } from 'chai';
import 'mocha';

describe('First test', 
  () => { 
    it('should return true', () => { 
      const result = helloTest();
      expect(result).to.equal(true); 
  }); 
});

Run the tests

For running the test, we’ll add a script in the package.json , register ts-node to run mocha and set up the path where the tests can be found, in this example it would be under tests directory:

"scripts": { "test": "mocha -r ts-node/register tests/**/*.spec.ts" },

We can now run the tests with npm:

npm run test

If everything goes well the test should run and you should see in the console output that it passes.

Conclusion

It is a matter of taste which testing library you choose, the most important thing is to have as many tests as possible, they help us create maintainable and stable applications which is desired in software development.

Follow me on Twitter for more software development tips.

The post Testing TypeScript with Mocha and Chai appeared first on 42 Coders.

Top comments (11)

Collapse
 
rodrigozan profile image
rodrigozan

Thanks for this post, helped me a lot

Collapse
 
geoffcapper profile image
Geoff

Thanks, this got me up and running with my TS tests. Mocha recommends against using arrow functions for tests (mochajs.org/#arrow-functions). As I'm only new at this, is there a reason to use them in this context?

Collapse
 
daniel_werner profile image
Daniel Werner

Thanks for your note! You are right, it is discouraged to use arrow functions in Mocha, however if you don't need to access the Mocha context (aka this), it works fine.
No special reason to use them here, just a shorter syntax :-)

Collapse
 
rcanu profile image
Reymart Canuel

Very straightforward and easy to follow! Thanks for this post @daniel_werner !

Collapse
 
daniel_werner profile image
Daniel Werner

You are welcome! If you are interested to learn more on testing with Mocha, check out my blog post series on the topic here: danielwerner.dev/category/testing-...

Collapse
 
willemodendaal profile image
Willem Odendaal

Is it possible to use babel/register instead of ts-node/register? Not really sure what the difference is between the two.

Collapse
 
diek profile image
diek

Thank you, i was just now refactoring the suit tests to typescript and i was wondering how to migrate this. Epic :)

Collapse
 
hansoncoding profile image
Hans

πŸ‘

Collapse
 
vm31 profile image
vm31

Good explanation helped me quick start testing with mocha and typescript .Thank you!

Collapse
 
bc profile image
Brian Canzanella

Awesome! This got me going in literally less than 2 minutes. Thanks!

Collapse
 
chidioguejiofor profile image
Chidiebere Ogujeiofor

This saved me immensely. Thanks a lot