Recently, I was planning to refactor the previous code, but I found that I did not write automatic test code, but manually, which was not good, so I learned Karma+Jasmine automated unit test, and I will try to write automatic unit test and test Istanbul code coverage in the future.

From what I understand, the Nodejs domain: Jasmine does unit testing, Karma automation does unit testing, Grunt starts Karma unified project management, Yeoman finally wraps into a project prototype template, NPM does nodeJS package dependency management, Bower does javascript package dependency management.

The introduction of Karma

Karma is a JavaScript Test Runner based on Node.js. The tool can be used to test all major Web browsers, integrated into the Continuous Integration (CI) tool, or used with other code editors. A powerful feature of the test tool is that it can monitor changes to the Watch file and then perform them on its own, displaying test results through console.log.

The installation of Karma

My development environment:

  • win7 64bit,
  • The node v8.11.2
  • NPM 5.6.0

Install karma, Jasmine- Core, Karma-Jasmine globally

npm install -g karma
npm install -g Jasime-core 
npm install -g karma-Jasmine
Copy the code

2. Install Karma, Jasmine- Core, karma -Jasmine for local office

npm install --save-dev karma
npm install --save-dev Jasime-core 
npm install --save-dev karma-Jasmine
Copy the code

3. Check whether the installation is successful

karma start
Copy the code

Karma + Jasmine configuration

karma init
Copy the code

Automated unit testing

1). Create a source file: a file used to achieve some business logic, that is, the JS script we usually write has a requirement to achieve the function of writing words backwards. For example: “ABCD” ==> “DCBA”

//src/index.js
function reverse(word){
    return word.split("").reverse().join("");
}
Copy the code

2). Create test files: test JS scripts that conform to the jasmineAPI

//test/test.js
describe("A suite of basic functions".function() {
    it("reverse word".function(){
        expect("DCBA").toEqual(reverse("ABCD"));
    });
});
Copy the code

3). Modify the karmap.conf. js configuration file we need to modify the files and exclude variables here

files: ['./src/*.js'],
exclude: ['karma.conf.js'].Copy the code

4). Start karma unit test automatic execution

karma start karma.conf.js
Copy the code

Karma and Istanbul code coverage

1, install Istanbul depends on karma-coverage

npm install -g karma-coverage
Copy the code

2, install Istanbul depends on karma-coverage

npm install --save-dev karma-coverage
Copy the code

3. Modify the configuration file karma. Conf. js

reporters: [‘progress’,’coverage’], preprocessors : {‘src.js’: ‘coverage’}, coverageReporter: { type : ‘html’, dir : ‘Coverage /’} 4, start karma start

Under the project directory to find the index. The HTML file, coverage/chrome/index. HTML

Next, we modify src.js to add an if branch

function reverse(word){
    if(word=='AAA') return "BBB";
    return word.split("").reverse().join("");
}
Copy the code

If you look at the coverage report, it’s a scary drop

In the pit of

Remember to install globally before you install locally, otherwise you will always get an error: Jasmine – Core module not found

Build Karma+Jasmine automated unit tests, welcome star, welcome exchange.

Project address KarmaDemo

Error: Cannot find module ‘jasmine-core’