I am using grunt-browserify + browserify-istanbul + grunt-contrib-jasmine + grunt-template-jasmine-istanbul as a solution. This solution also has excluded third-party libraries when creating source files using browserify .
Show the code first, I will explain later
grunt.initConfig({ browserify: { // build specs using browserify specs: { src: ["spec/**/*Spec.js"], dest: "spec/build/specs.js", options: { debug: true } }, // build source files using browserify and browserify-istanbul dev: { options: { debug: true, browserifyOptions: { standalone: 'abc' }, transform: [['browserify-istanbul', { ignore: ['**/node_modules/**'], // ignore third party libs defaultIgnore: true }]] }, src: ['abc.js'], dest: 'dist/abc.js' } }, connect: { server: { options: { port: 7000 } } }, // test using jasmine, generate coverage report using istanbul jasmine: { coverage: { src: ['dist/abc.js'], options: { junit: { path: 'bin/junit' }, host: 'http://localhost:7000/', specs: 'spec/build/specs.js', keepRunner: true, summary: true, template: require('grunt-template-jasmine-istanbul'), templateOptions: { replace: false, // *** this option is very important coverage: 'bin/coverage/coverage.json', report: [ { type: 'html', options: { dir: 'spec/coverage/html' } }] } } } } grunt.registerTask('specs', ['browserify:specs', 'browserify:dev', 'connect', 'jasmine']);
The steps for generating an Istanbul coverage report can be summarized in three:
- Device code
- Launch test
- Coverage Report
In our solution, we use browerify-istanbul in steps 1, grunt-contrib-jasmine and runt-template-jasmine-istanbul in steps 2 and 3.
browserify-istanbul will allow you to type code in the browser at the construction stage, so we can easily ignore third-party libraries. But grunt-template-jasmine-istanbul will program the code again. To avoid this, you can set replace to false in the options.
works:
JasmineOT
source share