Skip to content

Commit

Permalink
Add support for code coverage
Browse files Browse the repository at this point in the history
- proper Browserify support: karma-runner/karma-coverage#16 (comment)

Closes #21.
  • Loading branch information
niksy committed Jun 24, 2016
1 parent f96115c commit 525ab88
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
14 changes: 14 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand All @@ -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
Expand Down Expand Up @@ -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 ) {
Expand Down
7 changes: 5 additions & 2 deletions generators/app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion generators/app/templates/gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
node_modules/
npm-debug.log<% if ( (automatedTests && browserModule) || manualTests || integrationTests ) { %>
test-dist/<% } %>
test-dist/<% } %><% if ( automatedTests && codeCoverage ) { %>
coverage<% } %>
6 changes: 6 additions & 0 deletions generators/app/templates/istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instrumentation:
default-excludes: true
reporting:
print: summary
reports:
- html
16 changes: 15 additions & 1 deletion generators/app/templates/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
});
});

});

0 comments on commit 525ab88

Please sign in to comment.