Mocha is a javascript testing framework and Chai is an assertion library. The two are better used together, hence the name “matcha” (mocha is coffee). “Matcha” features: simple, Node and browser run.

Chai provides three assertions on the CHAI website

  • BDD: Behavior Driven Development, focusing on test logic
  • TDD: Test-driven Development, focusing on output

Simple uses of three assertions

const { should, expect, assert } = require('chai')
const { add, mul } = require('./sum')

// should needs to be called in advance
should()
add(4.5).should.equal(9)

expect(add(1.4)).equal(5)
expect(mul(2.3)).equal(6)

assert.equal(add(4.6), 10)
Copy the code

Mocha

Mocha is a feature-rich JavaScript testing framework that runs on Node and browsers, making asynchronous testing easy and fun. Mocha still has the flexibility to run accurate reports when errors are caught while running test cases. The default mode for Mocha is BDD.

Liverpoolfc.tv: mochajs.org/

The installation

npm i mocha -D

// package.json
"scripts": {..."test": "mocha test/mocha.js". },Copy the code
Basic usage
describe('#match'.() = > {
  describe('add'.() = > {
    it('should return 5 when 2 + 3'.() = > {
      expect(add(2.3), 5)})// it. Only Executes this case only
    // it. Skip this case
    it('should return 8 when 5 + 3'.() = > {
      expect(add(5.3), 8)
    })
  })
  describe('mul'.() = > {
    it('should return 12 when 4 * 3'.() = > {
      expect(mul(4.3), 12)})})})Copy the code

Describe is nested and describes whether the test case is correct.

It (info, function) Info is a descriptive description. Each IT corresponds to one unit test case.

Expect (mul(4, 3), 12) chai’s way of asserting

The output

The only() and skip() functions

Both the Describe and IT blocks allow calls to only() and skip() methods.

The only() method indicates that tests are executed only for that unit under the current parent Describe block.

The skip() method means that tests that do not execute the unit are skipped under the current parent Describe block.

When both the only() and skip() methods exist under a describe block, only the.only() method is executed.

Common Command Parameters

–recursive traverses all files in subdirectories

Mocha runs test scripts in the /test subdirectory by default.

By default, Mocha only executes test cases at the first level of the /test subdirectory.

Therefore, the -recursive parameter should be added so that test cases in all subdirectories can be executed.

mocha --recursive
Copy the code

-u TDD Executes the TDD mode

The default mocha mode is BDD. If you want to execute TDD test, you need to add parameters, such as:

mocha -u tdd test.js
Copy the code

–watch, -w Listens for script changes

The watch parameter is used to monitor the specified test script. When the script changes, Mocha is automatically run.

mocha --watch
Copy the code

The — Bail, -b parameter specifies that if any test case fails, the rest of the test cases will be stopped. This is useful for continuous integration.

mocha --bail
Copy the code

-timeout, -t Specifies the timeout threshold

Mocha defaults to a maximum of 2000 milliseconds per test case. If the execution is not complete after 2000 milliseconds, an error is reported. -t Specifies the timeout threshold.

mocha -t 5000 test.js
Copy the code