Skip to content

Commit

Permalink
feat(testUnit): use consistent /coverage directory for Node & Browser…
Browse files Browse the repository at this point in the history
… projects

Add integration test for coverage report to ensure it is created in the right place
  • Loading branch information
Brett Uglow committed Nov 27, 2016
1 parent 164877b commit 7b2bfca
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 27 deletions.
46 changes: 39 additions & 7 deletions lib/buildTool/npm/documentation/templates/config/prepublish.js.tpl
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
'use strict';

// START_CONFIT_GENERATED_CONTENT
// Update the swanky.config.yaml file's version number
// START_CONFIT_GENERATED_CONTENT<%
var configPath = paths.config.configDir + resources.documentation.configSubDir;
var relativePath = configPath.replace(/([^/]+)/g, '..');
%>
const path = require('path');
const basePath = path.join(__dirname, '/<%= relativePath %>');
const latestVersion = require('latest-version');
const fs = require('fs');
const pkgName = '<%- pkg.name %>';
const pkgRepo = '<%- pkg.repository.url.substr(pkg.repository.url.lastIndexOf('/') + 1).replace('.git', '') %>';

const IS_DRYRUN = process.argv.indexOf('-d') > -1; // Allow a dry-run to see what changes would be made

let FILES_TO_PROCESS = [
// Update the swanky.config.yaml file's version number and server path
{
name: '<%= documentation.srcDir %>swanky.config.yaml',
searchRE: /^version:.*$/m,
replacement: (version) => `version: ${version}`
},
{
name: '<%= documentation.srcDir %>swanky.config.yaml',
searchRE: /^serverPath:.*$/m,
replacement: () => `serverPath: ${pkgRepo}`
}
];

// In the next section you can modify FILES_TO_PROCESS to meet your needs...
// END_CONFIT_GENERATED_CONTENT



// START_CONFIT_GENERATED_CONTENT
latestVersion(pkgName).then(version => {
console.log(`Latest version of ${pkgName}: ${version}`);

// Read Swanky config YAML file, modify the version and server-path, then save it again
let text = fs.readFileSync('<%= documentation.srcDir %>swanky.config.yaml', 'utf8');
// Patch the files
FILES_TO_PROCESS.forEach(file => {
let fileName = path.join(basePath, file.name);
let text = fs.readFileSync(fileName, 'utf8');
text = text.replace(/^version:.*$/m, `version: ${version}`);
text = text.replace(/^serverPath:.*$/m, `serverPath: ${pkgRepo}`);
text = text.replace(file.searchRE, file.replacement(version));
fs.writeFileSync('<%= documentation.srcDir %>swanky.config.yaml', text, 'utf8');
if (IS_DRYRUN) {
console.info(`Updated version in ${file.name}:`);
console.info(text);
console.info('---------------');
} else {
fs.writeFileSync(fileName, text, 'utf8');
console.log(`Updated version in ${file.name}`);
}
});
});
// END_CONFIT_GENERATED_CONTENT

4 changes: 2 additions & 2 deletions lib/buildTool/npm/npmResources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ release:
- condition: <%- release.checkCodeCoverage && app.repositoryType === 'GitHub' %>
tasks:
- name: upload-coverage
tasks: ['cat <%- paths.output.reportDir %>lcov/lcov.info | ./node_modules/coveralls/bin/coveralls.js']
tasks: ['cat <%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>lcov/lcov.info | ./node_modules/coveralls/bin/coveralls.js']
description: Uploads code-coverage metrics to Coveralls.io
features:
- Setup - https://coveralls.zendesk.com/hc/en-us/articles/201347419-Coveralls-currently-supports
Expand Down Expand Up @@ -281,7 +281,7 @@ testUnit:
- Code coverage

- name: test:check-coverage
tasks: ['cross-env NODE_ENV=test istanbul check-coverage <%- paths.output.reportDir %>coverage.json --config <%- paths.config.configDir + resources.testUnit.configSubDir %>istanbul.js']
tasks: ['cross-env NODE_ENV=test istanbul check-coverage <%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>coverage.json --config <%- paths.config.configDir + resources.testUnit.configSubDir %>istanbul.js']
description:
features:

Expand Down
2 changes: 1 addition & 1 deletion lib/buildTool/npm/testUnit/templates/istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var coverageConfig = {
check: require('./thresholds.json'),
reporting: {
print: 'both',
dir: '<%= paths.output.reportDir %>',
dir: '<%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>',
reports: [
'cobertura',
'html',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let karmaConfig = {
reporters: ['progress', 'junit', 'coverage', 'threshold'],

coverageReporter: {
dir: '<%- paths.output.reportDir %>coverage',
dir: '<%- paths.output.reportDir + resources.testUnit.coverageReportSubDir %>',
reporters: [
{ type: 'cobertura', subdir: 'cobertura' },
{ type: 'lcovonly', subdir: 'lcov' },
Expand Down
4 changes: 2 additions & 2 deletions lib/buildTool/webpack/testUnit/templates/karma.conf.js.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var commonConfig = require('./karma.common.js');
var webpackHelpers = require('../webpack/webpackHelpers.js')();
var debugMode = process.argv.indexOf('--debug') > -1;
var noCoverage = process.argv.indexOf('--no-coverage') > -1;
var noThresholdCheck = process.argv.indexOf('--no-threshold-check') > -1;

function getConfitConfig(config) {
// level of logging
Expand Down Expand Up @@ -34,7 +34,7 @@ function getConfitConfig(config) {
});
}
if (noCoverage) {
if (noThresholdCheck) {
commonConfig.reporters = commonConfig.reporters.filter(function(reporter) {
return reporter !== 'threshold';
});
Expand Down
1 change: 1 addition & 0 deletions lib/core/resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ testSystem:

testUnit:
configSubDir: testUnit/
coverageReportSubDir: coverage/

testVisualRegression:
configSubDir: testVisualRegression/
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
"docs:serve": "http-server website/ -o",
"semantic-release": "semantic-release pre && npm publish && semantic-release post && npm run docs:publish",
"start": "npm run dev",
"test": "npm-run-all updateFixtures test:unit:once test:seq",
"test": "npm-run-all updateFixtures test:unit:once test:int",
"test:check-coverage": "cross-env NODE_ENV=test istanbul check-coverage reports/coverage.json --config config/testUnit/istanbul.js",
"test:coverage": "npm-run-all test:unit:once test:check-coverage --silent",
"test:seq": "node test/testRunner.js --sequence --MAX_LOG=true",
"test:int": "node test/testRunner.js --sequence --MAX_LOG=true",
"test:unit": "chokidar 'lib/**/*.js' 'test/**/*.js' -c 'npm run test:unit:once' --initial --silent",
"test:unit:once": "cross-env NODE_ENV=test istanbul cover --config config/testUnit/istanbul.js _mocha -- --opts config/testUnit/mocha.opts",
"updateFixtures": "node test/updateFixtures.js",
Expand Down
2 changes: 1 addition & 1 deletion test/spec/integration/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('test "' + fixtureFileName + '"', () => {
require('./testBuildServe')(confitConfig, SERVER_MAX_WAIT_TIME);
require('./testVerify')(confitConfig, srcDir, hasCSS);
// Execute unit tests but do not fail if coverage is too low
require('./testUnitTest')(confitConfig, unitTestDir, 'npm run test:unit:once -- --no-coverage', true);
require('./testUnitTest')(confitConfig, unitTestDir, 'npm run test:unit:once -- --no-threshold-check', true);
// Execute debug unit tests and ensure that there is NO coverage report
require('./testUnitTest')(confitConfig, unitTestDir, 'npm run test:unit:debug:once', false);
require('./testDocumentation')(confitConfig);
Expand Down
34 changes: 23 additions & 11 deletions test/spec/integration/testUnitTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,34 @@ function runCommand(cmd) {

module.exports = function(confitConfig, unitTestPath, commandToRun, hasCodeCoverage) {
describe(commandToRun, () => {
it(`should pass the unit tests in the sampleApp code ${hasCodeCoverage ? 'WITH' : 'WITHOUT'} code coverage`, function() {
it.only(`should pass the unit tests in the sampleApp code ${hasCodeCoverage ? 'WITH' : 'WITHOUT'} code coverage`, function() {
assert.doesNotThrow(() => runCommand(commandToRun));

if (hasCodeCoverage) {
let reportDir = path.join(process.env.TEST_DIR, confitConfig.paths.output.reportDir);
fs.readdirSync(reportDir + 'coverage/').forEach((file) => console.log(file));

assert(fs.existsSync(reportDir + 'coverage/') === true, 'Coverage dir exists');
assert(fs.existsSync(reportDir + 'coverage/lcov/lcov.info') === true, 'lcov/lcov.info exists');
fs.removeSync(reportDir); // Remove the directory
} else {
assert(fs.existsSync(reportDir + 'coverage/') === false, 'Coverage directory does NOT exist');
}
});

// We get a table of results like this, which we need to filter to find 'File ' and 'All files '
// ----------------|----------|----------|----------|----------|----------------|
// File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
// ----------------|----------|----------|----------|----------|----------------|
// demoModule/ | 80.95 | 100 | 33.33 | 76.47 | |
// app.js | 78.57 | 100 | 25 | 72.73 | 15,26,27 |
// demoModule.js | 85.71 | 100 | 50 | 83.33 | 9 |
// ----------------|----------|----------|----------|----------|----------------|
// All files | 80.95 | 100 | 33.33 | 76.47 | |
// ----------------|----------|----------|----------|----------|----------------|

it('should have 100% branch coverage for the test files', () => {
// We get a table of results like this, which we need to filter to find 'File ' and 'All files '
// ----------------|----------|----------|----------|----------|----------------|
// File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
// ----------------|----------|----------|----------|----------|----------------|
// demoModule/ | 80.95 | 100 | 33.33 | 76.47 | |
// app.js | 78.57 | 100 | 25 | 72.73 | 15,26,27 |
// demoModule.js | 85.71 | 100 | 50 | 83.33 | 9 |
// ----------------|----------|----------|----------|----------|----------------|
// All files | 80.95 | 100 | 33.33 | 76.47 | |
// ----------------|----------|----------|----------|----------|----------------|

let result = runCommand(commandToRun).toString();

console.log(result);
Expand Down

0 comments on commit 7b2bfca

Please sign in to comment.