Skip to content

Commit

Permalink
feat: simplify results output and add time entry
Browse files Browse the repository at this point in the history
  • Loading branch information
carpasse committed Nov 2, 2017
1 parent 146caae commit 11aba83
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 30 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
]
},
"dependencies": {
"chalk": "^2.3.0"
"chalk": "^2.3.0",
"ms": "^2.0.0"
}
}
40 changes: 27 additions & 13 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable id-match, class-methods-use-this, no-console */
const chalk = require('chalk');
const ms = require('ms');

class TapReporter {
constructor (globalConfig = {}, options = {}) {
Expand Down Expand Up @@ -36,23 +37,36 @@ class TapReporter {
}

onRunComplete (contexts, results) {
const {
numFailedTestSuites,
numFailedTests,
numPassedTestSuites,
numPassedTests,
numPendingTestSuites,
numPendingTests,
numTotalTestSuites,
numTotalTests,
startTime
} = results;
const skippedTestSuites = numPendingTestSuites > 0 ? `${chalk.yellow(`${numPendingTestSuites} skipped`)}, ` : '';
const skippedTests = numPendingTests > 0 ? `${chalk.yellow(`${numPendingTests} skipped`)}, ` : '';
const text = [];
const format = (msg, color, useColor = true) => {
if (useColor) {
return chalk[color](msg);
}

return msg;
};
this._shouldFail = numFailedTestSuites > 0 || numFailedTests > 0;

if (numFailedTestSuites > 0) {
text.push(`# testSuites: ${skippedTestSuites}${chalk.red(`${numFailedTestSuites} failed`)}, ${numTotalTestSuites} total`);
} else {
text.push(`# testSuites: ${skippedTestSuites}${chalk.green(`${numPassedTestSuites} passed`)}, ${numTotalTestSuites} total`);
}

this._shouldFail = results.numFailedTestSuites > 0 || results.numFailedTests > 0;
if (numFailedTests > 0) {
text.push(`# tests: ${skippedTests}${chalk.red(`${numFailedTests} failed`)}, ${numTotalTests} total`);
} else {
text.push(`# tests: ${skippedTests}${chalk.green(`${numPassedTests} passed`)}, ${numTotalTests} total`);
}

text.push(format(chalk.grey(`# Total tests: ${results.numTotalTests}\n`), this._shouldFail ? 'bgRed' : 'bgGreen'));
text.push(format(`# Passed suites: ${results.numPassedTestSuites}`, 'green', !this._shouldFail));
text.push(format(`# Failed suites: ${results.numFailedTestSuites}`, 'red', this._shouldFail));
text.push(format(`# Passed tests: ${results.numPassedTests}`, 'green', !this._shouldFail));
text.push(format(`# Failed tests: ${results.numFailedTests}`, 'red', this._shouldFail));
text.push(format(`# Skipped tests: ${results.numPendingTests}`, 'yellow', results.numPendingTests > 0));
text.push(`# time: ${ms(Date.now() - startTime)}`);

console.log(`\n${text.join('\n')}`);
}
Expand Down
110 changes: 94 additions & 16 deletions test/TapReporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,14 @@ test('TapReporter onRunComplete must set _shouldFail to true if a suite failed',
const tapReporter = new TapReporter();
const results = {
numFailedTests: 0,
numFailedTestSuites: 1
numFailedTestSuites: 1,
numPassedTests: 0,
numPassedTestSuites: 0,
numPendingTests: 0,
numPendingTestSuites: 0,
numTotalTests: 0,
numTotalTestSuites: 0,
startTime: Date.now() - 2000
};

tapReporter.onRunComplete({}, results);
Expand All @@ -214,36 +221,106 @@ test('TapReporter onRunComplete must set _shouldFail to true if a a test failed'
const tapReporter = new TapReporter();
const results = {
numFailedTests: 1,
numFailedTestSuites: 0
numFailedTestSuites: 0,
numPassedTests: 0,
numPassedTestSuites: 0,
numPendingTests: 0,
numPendingTestSuites: 0,
numTotalTests: 0,
numTotalTestSuites: 0,
startTime: Date.now() - 2000
};

tapReporter.onRunComplete({}, results);
expect(tapReporter._shouldFail).toBe(true);
});

test('TapReporter onRunComplete must output the Tap results', () => {
test('TapReporter onRunComplete all suites and tests pass', () => {
const tapReporter = new TapReporter();
const results = {
numFailedTests: 1,
numFailedTests: 0,
numFailedTestSuites: 0,
numPassedTests: 0,
numPassedTestSuites: 0,
numPassedTests: 10,
numPassedTestSuites: 2,
numPendingTests: 0,
numPendingTestSuites: 0,
numTotalTests: 1,
numTotalTestSuites: 1
numTotalTests: 10,
numTotalTestSuites: 2,
startTime: Date.now() - 2000
};

tapReporter.onRunComplete({}, results);

expect(console.log).toHaveBeenCalledWith(`
# Total tests: 1
# testSuites: 2 passed, 2 total
# tests: 10 passed, 10 total
# time: 2s`);
});

test('TapReporter onRunComplete some suites and tests fail', () => {
const tapReporter = new TapReporter();
const results = {
numFailedTests: 1,
numFailedTestSuites: 1,
numPassedTests: 10,
numPassedTestSuites: 2,
numPendingTests: 0,
numPendingTestSuites: 0,
numTotalTests: 10,
numTotalTestSuites: 2,
startTime: Date.now() - 2000
};

# Passed suites: 0
# Failed suites: 0
# Passed tests: 0
# Failed tests: 1
# Skipped tests: 0`);
tapReporter.onRunComplete({}, results);

expect(console.log).toHaveBeenCalledWith(`
# testSuites: 1 failed, 2 total
# tests: 1 failed, 10 total
# time: 2s`);
});

test('TapReporter onRunComplete 1 suite failed to execute', () => {
const tapReporter = new TapReporter();
const results = {
numFailedTests: 0,
numFailedTestSuites: 1,
numPassedTests: 10,
numPassedTestSuites: 1,
numPendingTests: 0,
numPendingTestSuites: 0,
numTotalTests: 10,
numTotalTestSuites: 2,
startTime: Date.now() - 2000
};

tapReporter.onRunComplete({}, results);

expect(console.log).toHaveBeenCalledWith(`
# testSuites: 1 failed, 2 total
# tests: 10 passed, 10 total
# time: 2s`);
});

test('TapReporter onRunComplete some suites and tests skipped', () => {
const tapReporter = new TapReporter();
const results = {
numFailedTests: 0,
numFailedTestSuites: 0,
numPassedTests: 5,
numPassedTestSuites: 1,
numPendingTests: 5,
numPendingTestSuites: 1,
numTotalTests: 10,
numTotalTestSuites: 2,
startTime: Date.now() - 2000
};

tapReporter.onRunComplete({}, results);

expect(console.log).toHaveBeenCalledWith(`
# testSuites: 1 skipped, 1 passed, 2 total
# tests: 5 skipped, 5 passed, 10 total
# time: 2s`);
});

test('TapReporter getLastError must return an error the run should fail and undefined otherwise', () => {
Expand All @@ -255,8 +332,9 @@ test('TapReporter getLastError must return an error the run should fail and unde
numPassedTestSuites: 0,
numPendingTests: 0,
numPendingTestSuites: 0,
numTotalTests: 1,
numTotalTestSuites: 1
numTotalTests: 0,
numTotalTestSuites: 0,
startTime: Date.now() - 2000
};

console.log.mockClear();
Expand Down

0 comments on commit 11aba83

Please sign in to comment.