Testing via Mocha and Chai in ES5 and ES6
Testing is a crucial part for developing, for JavaScript as well. Since the foundation theories of testing is almost the same regardless of the languages you are using. I will focus on introduction to Mocha and Chai, and environment setting up in this article. And for both ES5 and ES6. And the library we are using is Mocha and Chai. Mocha is a BDD testing framework and Chai is an assertion library. And at the end, I will talk about why I don’t use Jasmine for testing.
1. About the versions:
- “node.js”: “4.6.2”,
- “babel-preset-latest”: “6.16.0”,
- “babel-register”: “6.18.0”,
- “chai”: “3.5.0”,
- “mocha”: “3.2.0”
2. One minute for Mocha
There are so many little piece in Mocha, but you should know the following 3 for your first test. And conquer the rest at the homepage of Mocha, not that long.
2.1 Make your context via describe()
Your testing suites may consist of many parts. And you could distinguish them in a clear manner via describe()
, you needs 2 parameters for it, the first one is the description and the second one is a function which will contain your tests afterwards.
1 | describe("Start to test function A", function(){ |
If you have more cases to address, you can use another function called context()
to do the trick:
1 | describe("Start to test function A", function(){ |
2.2 Write your tests in it()
Your real code for tests locates in it()
, it has a same function signature as describe()
.
1 | describe("Start to test function A", function(){ |
Furthermore, if you are writing an asynchronous testing. Which means your testing code may ends later, but the whole block won’t wait your tests to finish. You can pass a done
to the callback function of it()
to fix it.
1 | describe("Start to test function A", function(){ |
2.3 You can hook your tests to do some preparation
With its default “BDD”-style interface, Mocha provides the hooks before(), after(), beforeEach(), and afterEach(). These should be used to set up preconditions and clean up after your tests.
1 | // Codes below are from Mochajs.org |
3. One minute to Chai
Chai focus on assertion, it provides with 3 types of assertions. Codes below are from http://Chaijs.com/guide/styles
3.1 Assert
This one has a similar feeling as the build-in assert
in node.js.
1 | var assert = require('chai').assert |
3.2 Expect
The BDD style assertion of expect()
enable you to represent your tests in a more human-reading-friendly way.
1 | var expect = require('chai').expect |
3.3 Should
Provides another approach to address your tests description. May have some issues when used with Internet Explorer, so be aware of browser compatibility.
1 | var should = require('chai').should() //actually call the function |
3.4 Languages chains
You can chain your description via the following methods. Here is a little disadvantage of using Chai
. Non-native speaker may have a trouble on combine these words to a sentence, but seems not a big deal, they are very basic English.
- to
- be
- been
- is
- that
- which
- and
- has
- have
- with
- at
- of
- same
4. Set up for ES5 Testing
Very easy to set up.
4.1 First, you install them
1 | # use NPM |
4.2 Second, import them in your tests and write a simple test
Create a file named test.js
locates in test/
in your project folder.
1 | var expect = require("chai").expect |
4.3 Third, write your function which needs to test.
The above tests will fail since there is no such addTwo()
. It doesn’t matter, we will add one now.
Create a file named index.js
locates in the root of your project folder with the following codes:
1 | export addTwo = function (num1, num2) { |
4.4 Fourth, add the following section to your package.json
.
If you already have a scripts
section, you can replace it with the following one.
1 | "scripts": { |
4.5 Run your tests via command line
1 | npm run test |
You can do the second trick since the test
is a reserved keyword of NPM.
5. See the fancy result
1 | > testMocha@1.0.0 test /Users/albertgao/codes/node/testMocha |
6. Set up for ES6 Testing
Testing for ES6 takes few more steps since you need to transpile your ES6 code to ES5.
6.1 First, you install them
1 | # use NPM |
6.2 Second, import them in your tests and write a simple test
Create a file named test.js
locates in test/
in your project folder.
1 | import chai from "chai" |
Something interesting happens here, I imported chai
, then apply its two functions to local variables. Why not just import {expect,should} from "chai"
, saddly, you can do it for expect
but not should
,
According to the official docs now:
It isn’t possible to chain a function call from an ES2015 import statement – it has to go on its own line.
But I saw a request on Github, which will enable import "chai/should" in the future
, hopefully in the 4.0 version. It mentioned in official docs. But I tried with no luck.
Second, I didn’t use arrow functions
here since the nature of the arrow function, you will lose the context binding, let’s say you want to use the built-in this.timeout(200)
to structure your tests. Then you shouldn’t use the arrow function even you are written in ES6
. But if not, feel free to use it.
6.3 Third, write your function which needs to test.
The above tests will fail since there is no such addTwo()
. It doesn’t matter, we will add one now.
Create a file named index.js
locates in the root of your project folder with the following codes:
1 | let addTwo = (num1, num2) => { |
6.4 Fourth, add the Babel support
1 | # use npm |
6.5 Fifth, create a .babelrc
file at the root of your project folder
With the following contents:
1 | { |
6.6 Sixth, add the following section to your package.json
.
If you already have a scripts
section, you can replace it with the following one.
1 | "scripts": { |
The idea here is easy, Mocha just uses babel-register
to transpile the file on the fly. Via this approach, you can write you tests and codes both in ES6, and tests them without pre-transpiling the code.
6.7 Run your tests via command line
1 | npm run test |
You can do the second trick since the test
is a reserved keyword of NPM.
7. See the fancy result
1 | > testMocha@1.0.0 test /Users/albertgao/codes/node/testMocha |
8. Why not Jasmine?
A little off-topic talking before ending. Why not Jasmine? It is good, it is full featured. Has built-in assert, mock(spy), ajax call, etc. And it’s easy to configure too. Sometimes just as same as Mocha, but with Mocha, you can choose your own favourite assertion library, like Chai
or better-assert
. And choose mock library like Sinon.js
, and even for the reporter part, you can use different reporter for a different outlook, even for exporting a HTML report.
So the main differences between Mocha
and Jasmine
is that you don’t this so called javascript fatigue
anymore with Jasmine
. But this is just why we love JavaScript
, right? (Not wierd, think about it. :)
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.