diff --git a/generators/app/index.js b/generators/app/index.js index 4959533..cabe5e0 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -192,6 +192,15 @@ module.exports = generators.Base.extend({ when: function ( answers ) { return answers.automatedTests; } + }, + { + type: 'confirm', + name: 'codeCoverage', + message: 'Do you need code coverage?', + 'default': false, + when: function ( answers ) { + return answers.automatedTests; + } } ], function ( answers ) { @@ -215,6 +224,7 @@ module.exports = generators.Base.extend({ automatedTests: answers.automatedTests, integrationTests: answers.integrationTests, testingInterface: answers.testingInterface, + codeCoverage: answers.codeCoverage, githubRepo: answers.githubRepo, keywords: keywords, version: pkg.version @@ -257,11 +267,15 @@ module.exports = generators.Base.extend({ cp('test/automated', 'test/automated'); cp('karma.conf.js', 'karma.conf.js'); } + if ( answers.codeCoverage ) { + cp('istanbul.yml', '.istanbul.yml'); + } } else { rm('.travis.yml'); rm('test/index.js'); rm('test/automated'); rm('karma.conf.js'); + rm('.istanbul.yml'); } if ( answers.manualTests || answers.integrationTests ) { diff --git a/generators/app/templates/_package.json b/generators/app/templates/_package.json index 67f97bb..9653636 100644 --- a/generators/app/templates/_package.json +++ b/generators/app/templates/_package.json @@ -17,7 +17,7 @@ "test:integration": "gulp test:prepare && wdio"<% } %><% if ( manualTests ) { %>, "test:manual:local": "gulp test:local:manual --watch"<% } %><% if ( (automatedTests && browserModule) || manualTests || integrationTests ) { %>, "test": "npm run lint<% if ( automatedTests && browserModule ) { %> && npm run test:automated<% } %><% if ( integrationTests ) { %> && npm run test:integration<% } %>"<% } else { %> - "test": "<% if ( automatedTests ) { %>eslint {index,test/**/*}.js && mocha test/**/*.js<% if ( testingInterface !== 'bdd' ) { %> --ui <%= testingInterface %><% } %><% } else { %>eslint index.js<% } %><% if ( browserModule && styles ) { %> && stylelint index.css<% } %>"<% } %> + "test": "<% if ( automatedTests ) { %>eslint {index,test/**/*}.js && <% if ( codeCoverage ) { %>istanbul cover _mocha<% } else { %>mocha<% } %> test/**/*.js<% if ( testingInterface !== 'bdd' ) { %><% if ( codeCoverage ) { %> --<% } %> --ui <%= testingInterface %><% } %><% } else { %>eslint index.js<% } %><% if ( browserModule && styles ) { %> && stylelint index.css<% } %>"<% } %> }<% if ( jqueryModule ) { %>, "dependencies": { "jquery": "^1.12.4" @@ -34,7 +34,10 @@ "karma-html2js-preprocessor": "^1.0.0", "karma-mocha": "^1.0.1", "karma-mocha-reporter": "^2.0.3"<% } %><% if ( automatedTests || integrationTests ) { %>, - "mocha": "^2.5.3"<% } %><% if ( manualTests || integrationTests ) { %>, + "mocha": "^2.5.3"<% } %><% if ( automatedTests && codeCoverage ) { %>, + "istanbul": "^0.4.3"<% } %><% if ( automatedTests && codeCoverage && browserModule ) { %>, + "browserify-istanbul": "^2.0.0", + "karma-coverage": "^1.0.0"<% } %><% if ( manualTests || integrationTests ) { %>, "browserify": "^13.0.1", "del": "^2.2.0", "event-stream": "^3.3.2", diff --git a/generators/app/templates/gitignore b/generators/app/templates/gitignore index 9c8db15..6700504 100644 --- a/generators/app/templates/gitignore +++ b/generators/app/templates/gitignore @@ -1,4 +1,5 @@ .DS_Store node_modules/ npm-debug.log<% if ( (automatedTests && browserModule) || manualTests || integrationTests ) { %> -test-dist/<% } %> +test-dist/<% } %><% if ( automatedTests && codeCoverage ) { %> +coverage<% } %> diff --git a/generators/app/templates/istanbul.yml b/generators/app/templates/istanbul.yml new file mode 100644 index 0000000..7c302c8 --- /dev/null +++ b/generators/app/templates/istanbul.yml @@ -0,0 +1,6 @@ +instrumentation: + default-excludes: true +reporting: + print: summary + reports: + - html diff --git a/generators/app/templates/karma.conf.js b/generators/app/templates/karma.conf.js index 098d77b..f989112 100644 --- a/generators/app/templates/karma.conf.js +++ b/generators/app/templates/karma.conf.js @@ -13,7 +13,7 @@ module.exports = function ( config ) { 'test/automated/**/*.html': ['html2js'], 'test/automated/**/*.js': ['browserify'] }, - reporters: ['mocha'], + reporters: ['mocha'<% if ( automatedTests && codeCoverage ) { %>, 'coverage'<% } %>], port: 9876, colors: true, logLevel: config.LOG_INFO, @@ -25,7 +25,21 @@ module.exports = function ( config ) { mocha: { ui: '<%= testingInterface %>' } + },<% if ( automatedTests && codeCoverage ) { %> + browserify: { + debug: true, + transform: [['browserify-istanbul', { defaultIgnore: true }]] }, + coverageReporter: { + reporters: [ + { + type: 'html' + }, + { + type: 'text-summary' + } + ] + },<% } %> customLaunchers: { 'BS-Chrome': { base: 'BrowserStack', diff --git a/test/index.js b/test/index.js index 56a7231..a91147c 100644 --- a/test/index.js +++ b/test/index.js @@ -440,3 +440,33 @@ describe('CLI', function () { }); }); + +describe('code coverage', function () { + + before(function () { + return helpers.run(path.join(__dirname, '../generators/app')) + .withPrompts({ + automatedTests: true, + codeCoverage: true + }) + .toPromise(); + }); + + it('creates files', function () { + assert.file([ + '.istanbul.yml' + ]); + }); + + it('package.json', function () { + assert.JSONFileContent('package.json', { + scripts: { + test: 'eslint {index,test/**/*}.js && istanbul cover _mocha test/**/*.js' + }, + devDependencies: { + istanbul: '^0.4.3' + } + }); + }); + +});