Basic knowledge of

  • Karma serves as a browser test environment, Mocha as a real test framework, and Chai as an assertion library

Test case basis

  • The Describe block is called a Test Suite, which represents a set of related tests. It is a function whose first argument is the name of the test suite (” test of the addition function “) and the second argument is a function that is actually executed. The describe hook:

    'describe('hooks', function() {before(function() {// run before all test cases in this block}); After (function() {// Execute after all test cases in this block}); BeforeEach (function() {// Execute beforeEach test case in this block}); AfterEach (function() {// Execute afterEach test case in this block}); // test cases }); `
  • An IT block, called a “test case,” represents a single test and is the smallest unit of tests. It is also a function, with the first argument being the name of the test case and the second argument being a function that is actually executed.

    Expect (add(1, 1)).to be equal(2); expect(add(1, 1)).to be equal(2); }); });


  • Assertions library: should.js – BDD style shown throughout these docs expect.js – expect() style assertions chai – expect(), assert() and should-style assertions better-assert – C-style self-documenting assert() unexpected – “the extensible BDD assertion toolkit”

Karma environment building (Karma + Mocha + Chai + Webpack)

  • Dependent module installation:
npm install karma-cli -g
cnpm install karma karma-chai karma-mocha karma-webpack webpack babel-loader babel-core mocha chai karma-chrome-launcher --save-dev
  • Generate the karma.conf.js file
 karma init karma.conf.js
  • Modify the karmat.conf.js configuration as required by the project
  • To enable the karma
karma start karma.conf.js

Note: Included :false in config file: files, the test file needs to be loaded manually. If the test file is not loaded automatically, it will not automatically test the use of coverage.

        test: /\.js$/,
        loader: 'babel-loader',



Chai document

Karma – coverage document

The karma of notes

Mocha notes

Chai notes