JavaScript coverage statistics Implementation
Main requirements 1. Support for browser & nodejs
Because javascript can be run in both the browser environment and nodejs environment, you need to be able to measure the unit test coverage in both environments.
2. Transparent and seamless
When writing unit test cases, you do not need to write more code to support coverage rate statistics. You can directly calculate the coverage rate without modifying the previous write cases.
Principle
There are few articles about javascript coverage. The figure below is based on reading the Open Source javascript coverage tool istanbul and the coverage plug-in Karma-coverage of the open source test framework karma. The core idea of javascript coverage rate statistics is to inject statistical code into the corresponding position of the source code. After the code is run, the program running path is determined based on the statistical code statistics, and finally the coverage rate statistics report is generated.
1. Conversion (instrument)
- Use the open-source tool Esprima for source code syntax generation scan ky "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vc8bGk + kernel + DTprXEyKu + 1 rHkwb + Kernel + 3cirvtax5MG/example/qtS0uaS + example/bbUPGNvZGU + example + zKtc/Wo6y688Pmu + HP6s + example/ examples/examples/samples + rPJsuLK1LGouOYKPHA + samples + iu + g50t0000ku + samples/Cw + samples + oaM8L3A + samples + CtK7uPbA/samples = "brush: java; ">
//source codefunction abs(num){ if(abs > 0) return num; else return -num;}
//instrumented codevar __cov_iypKC$dWI6uJFmvxThycaA = (Function('return this'))();if (!__cov_iypKC$dWI6uJFmvxThycaA.__coverage__) { __cov_iypKC$dWI6uJFmvxThycaA.__coverage__ = {}; }__cov_iypKC$dWI6uJFmvxThycaA = __cov_iypKC$dWI6uJFmvxThycaA.__coverage__;if (!(__cov_iypKC$dWI6uJFmvxThycaA['/Users/lonfee88/Codes/testframe/coverage-jasmine-istanbul-karma/abs.js'])) { __cov_iypKC$dWI6uJFmvxThycaA['/Users/lonfee88/Codes/testframe/coverage-jasmine-istanbul-karma/abs.js'] = {"path":"/Users/lonfee88/Codes/testframe/coverage-jasmine-istanbul-karma/abs.js","s":{"1":1,"2":0,"3":0,"4":0},"b":{"1":[0,0]},"f":{"1":0},"fnMap":{"1":{"name":"abs","line":1,"loc":{"start":{"line":1,"column":-15},"end":{"line":1,"column":17}}}},"statementMap":{"1":{"start":{"line":1,"column":-15},"end":{"line":6,"column":1}},"2":{"start":{"line":2,"column":1},"end":{"line":5,"column":14}},"3":{"start":{"line":3,"column":2},"end":{"line":3,"column":13}},"4":{"start":{"line":5,"column":2},"end":{"line":5,"column":14}}},"branchMap":{"1":{"line":2,"type":"if","locations":[{"start":{"line":2,"column":1},"end":{"line":2,"column":1}},{"start":{"line":2,"column":1},"end":{"line":2,"column":1}}]}}};}__cov_iypKC$dWI6uJFmvxThycaA = __cov_iypKC$dWI6uJFmvxThycaA['/Users/lonfee88/Codes/testframe/coverage-jasmine-istanbul-karma/abs.js'];function abs(num){__cov_iypKC$dWI6uJFmvxThycaA.f['1']++;__cov_iypKC$dWI6uJFmvxThycaA.s['2']++;if(abs>0){__cov_iypKC$dWI6uJFmvxThycaA.b['1'][0]++;__cov_iypKC$dWI6uJFmvxThycaA.s['3']++;return num;}else{__cov_iypKC$dWI6uJFmvxThycaA.b['1'][1]++;__cov_iypKC$dWI6uJFmvxThycaA.s['4']++;return-num;}}
Node. js integration coverage
The hook can directly and seamlessly load the converted code, and hook the following two statements:
require
vm.createScript
The code to hook require is throughModule._extensions['.js']Implemented by assigning values:
Function hookRequire (matcher, transformer, options) {options = options | |{}; var fn = transformFn (matcher, transformer, options. verbose), postLoadHook = options. postLoadHook & typeof options. postLoadHook = 'function '? Options. postLoadHook: null; Module. _ extensions ['. js'] = function (module, filename) {var ret = fn (fs. readFileSync (filename, 'utf8'), filename); if (ret. changed) {// load the code after the instrument and run the module. _ compile (ret. code, filename);} else {// load the original code and run originalLoader (module, filename);} if (postLoadHook) {postLoadHook (filename );}};}
Hook makes coverage integration simple, and does not even need to write code. For example, Mocha coverage integration only needs to use the following call method:
istanbul cover _mocha -- -R spec test/spec
Browser integration coverage
Browser integration coverage is a little tricky. Fortunately, istanbul provides APIs:
- Convert the code (call the istanbul Instrumenter Interface)
- Send the instrumented code to the browser (implemented by yourself)
- Send the execution result containing the coverage information back to the server (implemented by yourself)
- Generate a coverage report based on the returned Coverage Information (call the Reporter interface of istanbul)