In this post we will learn
Let's us start with a brief about Mocha.
mkdir /path/to/newfolder
node -v #verify node version
npm -v #verify npm version
npm install mocha -g #install mocha globally
mocha -version #verify is installed
describe() - Simple way to group our tests in Mocha. Feature to create a series of tests. Takes two arguments
it() - Way to describe the individual test case. Nested within the describe() block.
User assertion module
var assert = require('assert');
assert.equal(a,b); // Check if both equal
What are hooks?
What are test features? features to tell which tests should be executed and which not.
4 passing (521ms)
1 pending
1 failing
Retry: Tell Mocha to execute the failed tests several times. Used in end-to-end tests. Re-runs beforeEach and afterEach hooks but NOT before or after hooks.
Timeouts: To explicitly tell Mocha to timeout. 3 levels:
this.timeout(300); // time in miliseconds
setTimeout(done, 300) // remember to pass done to function
Mocha reporters are mostly terminal based.
SPEC: Default reporter. The "spec" reported outputs a hierarchical view according to test cases.
DOT MATRIX: Series of characters which represent the test cases. Failures (!), pending tests (,) in blue, and slow tests (,) in yellow.
DOT MATRIX
pecobe ❯❯ mocha .\helloMocha.js --reporter dot
....,
5 passing (11ms)
1 pending
1 -_-_-_-^|__( o .o)
-_-_-_- "" ""
5 passing (30ms)
1 pending
pecobe ❯❯ mocha .\helloMocha.js --reporter json
{
"stats": {
"suites": 2,
"tests": 6,
"passes": 5,
"pending": 1,
"failures": 0,
"start": "2022-10-16T03:54:04.041Z",
"end": "2022-10-16T03:54:04.047Z",
"duration": 6
},
"tests": [
{
"title": "Addition of two numbers",
"fullTitle": "Mathematical Operations - Test Suite Addition of two numbers",
"file": "D:\\Documents\\Git\\mochatest\\helloMocha.js",
"duration": 0,
"currentRetry": 0,
"speed": "fast",
"err": {}
},
...