Testing

Testing TypeScript with Mocha and Chai

Daniel Verner -

Introduction
I am a huge fan of TDD, already wrote a couple of articles on this topic. In one of my recent projects, I used Angular as a frontend 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 the Chai assertion library. Let’s see how to set up the testing environment for TypeScript.

Installation
As a first 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 our function works as expected. Obviously, in real TDD, we should write the test first and the function afterward, 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 are desired in software development.

Tags: testing · chai · mocha · tdd · typescript

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • Happy birthday 🎁🎈🎂 Filip - #

  • Another day another #mandarinacakeshop 🎂 😀 - #

  • Happy Birthday Ognjen! And marry Christmas to all other 🎄#notacakeshop - #

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan 🥂 - #

  • #happybirthday Lidija 🍾 - #

  • Say hi 👋 to our newest team member ☕️ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday 🎂 Miloš - #

  • Happy Birthday 🎂Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday 🎂 Ognjen - #

  • Wish you all a merry Christmas 🎄🎁 - #

See more!

© 2024 42coders All rights reserved.