QA Notes | Mocha

How to start working with Mocha. Basic introduction.

Posted by PeCoBe on 2022-10-16 ·

Mocha is a (delicious) JavaScript test framework running on Node.js and in the browser.

In this post we will learn

  • What is Mocha, how to install?
  • Function calls
  • Mocha Hooks
  • Test Features
  • Retry and Timeouts
  • Reporters

Let's us start with a brief about Mocha.

  • Testing framework for JavaScript
  • Organize and execute test cases
  • Makes asynchronous testing to be simple and fun to execute

Pre-requisites for Mocha

  1. NodeJS
  2. Node Package Manager NPM
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

Function calls

  • describe() - Simple way to group our tests in Mocha. Feature to create a series of tests. Takes two arguments

    1. Name/Descriotion of the test group
    2. Call back function.
  • it() - Way to describe the individual test case. Nested within the describe() block.

Assertions

User assertion module

var assert = require('assert');
assert.equal(a,b); // Check if both equal

Mocha Hooks

What are hooks?

  • before() : runs before all tests in code block
  • after() : runs after all tests in code block
  • beforeEach() : runs before each test in code block
  • afterEach() : runs after each test in code block

Test Features

What are test features? features to tell which tests should be executed and which not.

  • Exclusive tests: Run only the specified suite or test-case by using .only() to the function.
  • Inclusive tests: Ignore the suites and test cases using .skip() to the function.
  • Pending tests: Tests will be included in the test results, marked as pending. Not considered a failed test.
  4 passing (521ms)
  1 pending
  1 failing

Retry and Timeouts functions

  • 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:

    1. Suite-level
    2. Test-level
    3. Hook-level
this.timeout(300); // time in miliseconds
setTimeout(done, 300) // remember to pass done to function

Reporters

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
  • NYAN
 1   -_-_-_-^|__( o .o)
     -_-_-_-  ""  ""
     
  5 passing (30ms)
  1 pending
  • JSON
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": {}
    },
    ...
  • Mocha
  • What is Mocha, how to install?
  • Function calls
  • Mocha Hooks
  • Test Features
  • Retry and Timeouts
  • Reporters