javascript - Karma Coverage + RequireJS: Misleading coverage reports -



javascript - Karma Coverage + RequireJS: Misleading coverage reports -

today i've integrated karma coverage existing requirejs application. added karma-requirejs plugin , able coverage reports.

initially study good, 100% coverage. when analyzed results noticed lot of missing files "src" folder led extremely report.

it turns out coverage applying analysis "src" files have corresponding spec (because utilize require('somefilefromsrcfolder') within spec).

question: there way coverage analyze files "src" folder?

karma-conf.js

module.exports = function (config) { config.set({ basepath: '../', autowatch: true, singlerun: false, frameworks: ['jasmine', 'requirejs'], browsers: ['phantomjs', 'chrome'], loglevel: config.log_error, files: [ {pattern: 'vendor/**/*.js', included: false}, {pattern: 'vendor/**/*.html', included: false}, {pattern: 'src/**/*.js', included: false}, {pattern: 'src/**/*.html', included: false}, {pattern: 'tests/mock/**/*.js', included: false}, {pattern: 'tests/**/*specs.js', included: false}, 'tests/test-require.js' ], // list of files exclude exclude: [ 'src/app-require.js' ], reporters: ['progress', 'coverage'], preprocessors: { 'src/**/*.js': ['coverage'] } }); };

test-require.js

var alltestfiles = []; var test_regexp = /specs\.js$/; object.keys(window.__karma__.files).foreach(function (file) { if (test_regexp.test(file)) { alltestfiles.push(file); } }); require.config({ baseurl: '/base/', paths: { ... }, deps: alltestfiles, callback: window.__karma__.start(), waitseconds: 20 });

ok,

after trying little bit able come solution seems prepare problem. i'm still not sure if best solution post here did can gather feedbacks.

basically, had alter load mechanism in test-require.js include packages default. updated test-require.js should follows:

updated test-require.js

var alltestfiles = []; var modules = []; var test_regexp = /specs\.js$/; var src_regexp = /src\//; var js_regexp = /\.js$/; /** * function converts given js path requirejs module */ var jstomodule = function (path) { homecoming path.replace(/^\/base\//, '').replace(/\.js$/, ''); }; object.keys(window.__karma__.files).foreach(function (file) { if (test_regexp.test(file)) { alltestfiles.push(file); } else if (src_regexp.test(file) && js_regexp.test(file)) { modules.push(jstomodule(file)); } }); var starttest = function () { //loading existing requirejs src modules before //triggering karma test require(modules, function () { window.__karma__.start(); }); }; require.config({ baseurl: '/base/', paths: { .... }, // dynamically load test files deps: alltestfiles, callback: starttest, waitseconds: 20 });

now, when run test tests, coverage includes src modules , can consistent , accurate reports.

javascript requirejs karma-runner karma-coverage

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -