Before encountering Blanket. js, I used this jscover for code coverage testing.
Jscover
The advantage of using jscover is that the java version is used directly without compilation. The disadvantage is that the java environment dependency is required.
Use Makefile to drive the entire test execution, such as urllib/Makefile
TESTS = test/*.test.jsREPORTER = specTIMEOUT = 20000JSCOVERAGE = ./node_modules/jscover/bin/jscovertest: @NODE_ENV=test ./node_modules/mocha/bin/mocha \ --reporter $(REPORTER) \ --timeout $(TIMEOUT) \ $(TESTS)test-cov: @rm -rf ./lib-cov @$(JSCOVERAGE) lib lib-cov @URLLIB_COV=1 $(MAKE) test REPORTER=dot @URLLIB_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html.PHONY: test test-cov
Run the code coverage test:
$ make test-cov
Open coverage.html to see the coverage.
Blanket. js
One day on github, I saw Blanket. js, which was an unexpected result and finally got rid of Code Conversion dependencies.
Blanket. js is an easy to install, easy to configure, and easy to use JavaScript code coverage library that works both in-browser and with nodejs.
After using balancer. js, you no longer need to convert the code into a file or use java dependencies.
Take urllib/Makefile as an example:
TESTS = test/*.test.jsREPORTER = specTIMEOUT = 20000MOCHA_OPTS =test: @NODE_ENV=test ./node_modules/mocha/bin/mocha \ --reporter $(REPORTER) \ --timeout $(TIMEOUT) \ $(MOCHA_OPTS) \ $(TESTS)test-cov: @URLLIB_COV=1 $(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=dot @URLLIB_COV=1 $(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=html-cov > coverage.html.PHONY: test test-cov
Refer to Getting Started Guide (nodejs version)
In package. json, add the code matching mode of blanket. Make sure you want to convert the code in that path.
"scripts": { "test": "make test", "blanket": { "pattern": "urllib/lib" }},
The command execution remains the same:
$ make test-cov
coverage.html
The results are the same as those of the jscover report. If no file is generated, you do not need to install java on the server.
Project
For non-lib-type projects, such as common application projects, code coverage testing can also be done well using Blanket. js.
Take urlrar as an example:
Markfile
TESTS = test/*.test.jsREPORTER = specTIMEOUT = 10000MOCHA_OPTS =install: @npm installtest: install @NODE_ENV=test ./node_modules/mocha/bin/mocha \ --reporter $(REPORTER) \ --timeout $(TIMEOUT) \ $(MOCHA_OPTS) \ $(TESTS)test-cov: @URLRAR_COV=1 $(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=html-cov > coverage.html @URLRAR_COV=1 $(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=travis-covtest-all: test test-cov.PHONY: test-cov test test-all
Package. json
"devDependencies": { "travis-cov": "*", "blanket": "*", // ... }, "scripts": { "test": "make test-all", "blanket": { "pattern": "//^((?!(node_modules|test)).)*$/", "data-cover-flags": { "debug": false } }, "travis-cov": { "threshold": 93 } },
$ npm test
Running result: contains test results and code coverage.
1 Love
^_^ I hope this article will be useful to you.