Mocha + Chai unit tests

Reference link: Test framework Mocha instance tutorial assertion library using Chai

Mainly for personal practice record storage, the following will show some or focus easy to mistake points.

Wildcard characters (it is recommended not to use the {} pattern when there is only one file).

$ mocha spec/{my,awesome}.js
$ mocha test/unit/*.js
$ mocha 'test/**/*.@(js|jsx)'// Also supports node wildcards. If there is only one file, it needs to be added, such as mocha test/{cf,}.js, otherwise an error will be reported. Warning: Could not find any test files matching pattern: test/{cf}.js No test files foundCopy the code

Recursive execution (By default, Mocha will execute files in the test directory under the current directory)

If you want to execute other embedded files, such as test/cf/**.js; test/gg/tt/**.js; This default use of mocha commands is not executed because they are not in the test directory. You can use: mocha --recursive, which means you can recursively execute all files in the test directory, including deeply nested files.Copy the code

Display report results (--reporterParameter specifies the format of the test report. The default isspecFormat.)

$mocha # is equivalent to $mocha -- Reporter SpecCopy the code

The –reporters parameter displays all built-in report formats.

$ mocha --reporters
Copy the code

Generate report results (usingmochawesomeModule that can generate reports in beautiful HTML format.

$ npm install --save-dev mochawesome $ .. /node_modules/.bin/mocha --reporter mochawesomeCopy the code

In the above code, the mocha command uses the version installed within the project, rather than the globally installed version, because the mochawesome module is installed within the project.

A report of the test results is then generated in the Mochaawesome – Reports subdirectory.

Configuration file mocha.opts

Mocha allows you to place the configuration file mocha.opts under the test directory and write the command-line arguments in it. Go to the demo03 directory and run the following command.

$ mocha --recursive --reporter tap
Copy the code

The above command takes two arguments –recursive and –reporter tap.

Then, write the two parameters to the mocha.opts file in the test directory.

--reporter tap
--recursive
Copy the code

Then, execute mocha to get the same effect as the first command.

$ mocha
Copy the code

If the test case is not in the Test subdirectory, you can write the following to mocha.opts.

otherDirTests
--recursive
Copy the code

The above code specifies to run test scripts in the otherDirTests directory and its subdirectories.

ES6 test

If the test script is written in ES6, you need to transcode the test with Babel before running it. Go to the demo04 directory and open the test/add.test.js file. You can see that the test case was written in ES6.

import add from '.. /src/add.js';
import chai from 'chai';

let expect = chai.expect;

describe('Tests for addition functions'.function() {
  it('1 plus 1 should be 2'..function() {
    expect(add(1.1)).to.be.equal(2);
  });
});
Copy the code

ES6 transcoding requires the installation of Babel.

$ npm install babel-core babel-preset-es2015 --save-dev
Copy the code

Next, create a.babelrc configuration file in the project directory.

{
  "presets": [ "es2015"]}Copy the code

Finally, the –compilers parameter is used to specify the transcoder of the test script.

$ ../node_modules/mocha/bin/mocha --compilers js:babel-core/register
Copy the code

In the above code, the –compilers parameter is followed by a string separated by colons. The left side of the colons is the file suffix and the right side is the module name used to process such files. Before running the test, use the babel-core/register module to process the.js file. Since the transcoder here is installed within the project, use the Mocha installed within the project; If the transcoder is installed globally, you can use the global Mocha.

This can be used in conjunction with mocha.opts, or with custom commands in package.json.

Package. json (use:npm run test)

"scripts": {
    "test": "mocha --recursive --compilers js:babel-core/register"
 }
Copy the code

Compilers indicate during use that they will be removed in future versions and are recommended to use the following method!

"scripts": {
    "test": "mocha --recursive --require babel-core/register"
 }
Copy the code

Mocha. Opts (Usage:mocha)

--reporter spec
--recursive
--compilers js:babel-core/register
Copy the code

Compilers indicate during use that they will be removed in future versions and are recommended to use the following method!

--reporter spec
--recursive
--require babel-core/register
Copy the code