Skip to content

Commit

Permalink
fix: make snapshot summary display correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Dalecky authored and Vadim Dalecky committed Nov 8, 2017
1 parent 5e3ac85 commit 6f30c01
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 34 deletions.
73 changes: 58 additions & 15 deletions src/LineWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ const REG_ERROR = /^\s*Error:\s*/;
const MDASH = '\u2014';
const CIRCLE = '●';

const FAIL_TEXT = 'FAIL';
const PASS_TEXT = 'PASS';

const FAIL = chalk.supportsColor ?
chalk`{reset.inverse.bold.red ${FAIL_TEXT} }` :
` ${FAIL_TEXT} `;

const PASS = chalk.supportsColor ?
chalk`{reset.inverse.bold.green ${PASS_TEXT} }` :
` ${PASS_TEXT} `;

const formatComment = (line) => chalk`{hidden #} ${line}`;
const formatFailureMessageTraceLine = (description, relativeFilePath, row, column) =>
chalk`${description}({cyan ${relativeFilePath}}:{black.bold ${row}}:{black.bold ${column}})`;
Expand Down Expand Up @@ -52,29 +63,63 @@ class LineWriter {
this.comment(chalk`{black.bold ${keyFormatted}} ${value}`);
}

stats (name, failed, skipped, passed, total, names = {
failed: 'failed',
passed: 'passed',
skipped: 'skipped',
total: 'total'
}) {
keyValueList (key, list) {
let value = '';

for (const [label, style, num] of list) {
value += (value ? ', ' : '') + chalk`{${style} ${num} ${label}}`;
}

this.keyValue(key, value);
}

stats (name, failed, skipped, passed, total) {
const list = [];

if (total) {
if (failed) {
value += (value ? ', ' : '') + chalk`{red.bold ${failed} ${names.failed}}`;
list.push(['failed', 'red.bold', failed]);
}

if (skipped) {
value += (value ? ', ' : '') + chalk`{yellow.bold ${skipped} ${names.skipped}}`;
list.push(['skipped', 'yellow.bold', skipped]);
}

if (passed) {
list.push(['passed', 'green.bold', passed]);
}
}

list.push(['total', 'reset', total]);
this.keyValueList(name, list);
}

snapshots (failed, updated, added, passed, total) {
if (!total) {
return;
}

const list = [];

if (failed) {
list.push(['failed', 'red.bold', failed]);
}

if (updated) {
list.push(['updated', 'yellow.bold', updated]);
}

if (added) {
list.push(['added', 'green.bold', added]);
}

value += (value ? ', ' : '') + chalk`{green.bold ${passed} ${names.passed}}`;
if (passed) {
list.push(['passed', 'green.bold', passed]);
}

value += `${total ? ', ' : ''}${total} ${names.total}`;
list.push(['total', 'reset', total]);

this.keyValue(name, value);
this.keyValueList('Snapshots', list);
}

result (okNotOK, title) {
Expand Down Expand Up @@ -174,9 +219,7 @@ class LineWriter {
}

suite (isFail, dir, base) {
const label = isFail ?
chalk`{bgRed.rgb(255,255,255).bold FAIL }` :
chalk`{bgGreen.rgb(255,255,255).bold PASS }`;
const label = isFail ? FAIL : PASS;

this.comment(chalk`${label} {grey ${this.getPathRelativeToRoot(dir)}${path.sep}}{bold ${base}}`);
}
Expand All @@ -186,7 +229,7 @@ class LineWriter {
throw new Error('TAP test plan can be written only once.');
}

this.logger.log(chalk`{bgBlack.rgb(255,255,255) 1..${count}}`);
this.logger.log(chalk`{reset.inverse 1..${count}}`);
this.planWritten = true;
}
}
Expand Down
42 changes: 23 additions & 19 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,34 @@ class TapReporter {
this.onRunStartOptions = options;
}

onRunComplete (contexts, results) {
onRunComplete (contexts, aggregatedResults) {
const {estimatedTime} = this.onRunStartOptions;
const {
numFailedTestSuites,
numFailedTests,
numPassedTestSuites,
numPassedTests,
numPendingTestSuites,
numPendingTests,
numTotalTestSuites,
numTotalTests,
snapshot,
startTime
} = results;

this[sShouldFail] = numFailedTestSuites > 0 || numFailedTests > 0;

const snapshotResults = aggregatedResults.snapshot;
const snapshotsAdded = snapshotResults.added;
const snapshotsFailed = snapshotResults.unmatched;
const snapshotsPassed = snapshotResults.matched;
const snapshotsTotal = snapshotResults.total;
const snapshotsUpdated = snapshotResults.updated;
const suitesFailed = aggregatedResults.numFailedTestSuites;
const suitesPassed = aggregatedResults.numPassedTestSuites;
const suitesPending = aggregatedResults.numPendingTestSuites;
const suitesTotal = aggregatedResults.numTotalTestSuites;
const testsFailed = aggregatedResults.numFailedTests;
const testsPassed = aggregatedResults.numPassedTests;
const testsPending = aggregatedResults.numPendingTests;
const testsTotal = aggregatedResults.numTotalTests;
const startTime = aggregatedResults.startTime;

this[sShouldFail] = testsFailed > 0 || suitesFailed > 0;

this.writer.blank();
this.writer.plan();
this.writer.blank();
this.writer.stats('Test Suites', numFailedTestSuites, numPendingTestSuites, numPassedTestSuites, numTotalTestSuites);
this.writer.stats('Tests', numFailedTests, numPendingTests, numPassedTests, numTotalTests);
if (snapshot) {
this.writer.stats('Snapshots', snapshot.total - snapshot.matched - snapshot.added, 0, snapshot.matched + snapshot.added, snapshot.total);
this.writer.stats('Test Suites', suitesFailed, suitesPending, suitesPassed, suitesTotal);
this.writer.stats('Tests', testsFailed, testsPending, testsPassed, testsTotal);
if (snapshotsTotal) {
this.writer.snapshots(snapshotsFailed, snapshotsUpdated, snapshotsAdded, snapshotsPassed, snapshotsTotal);
}
this.writer.keyValue('Time', `${((Date.now() - startTime) / 1e3).toFixed(3)}s, estimated ${estimatedTime}s`);
this.writer.commentLight('Ran all test suites.');
Expand Down
14 changes: 14 additions & 0 deletions test/LineWriter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ describe('LiveWriter', () => {
});
});

describe('.snapshots()', () => {
describe('when all values are greater than zero', () => {
test('prints them all', () => {
const writer = create();

writer.keyValue = jest.fn();
writer.snapshots(1, 1, 1, 1, 4);

expect(writer.keyValue).toHaveBeenCalledTimes(1);
expect(writer.keyValue.mock.calls[0]).toMatchSnapshot();
});
});
});

describe('.result()', () => {
test('logs passed test', () => {
const writer = create();
Expand Down
5 changes: 5 additions & 0 deletions test/TapReporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('TapReporter', () => {
numPendingTestSuites: 0,
numTotalTests: 0,
numTotalTestSuites: 0,
snapshot: {},
startTime: Date.now() - 2000
};

Expand Down Expand Up @@ -115,6 +116,7 @@ describe('TapReporter', () => {
numPendingTestSuites: 0,
numTotalTests: 10,
numTotalTestSuites: 2,
snapshot: {},
startTime: Date.now() - 2000
};

Expand All @@ -137,6 +139,7 @@ describe('TapReporter', () => {
numPendingTestSuites: 0,
numTotalTests: 10,
numTotalTestSuites: 2,
snapshot: {},
startTime: Date.now() - 2000
};

Expand All @@ -159,6 +162,7 @@ describe('TapReporter', () => {
numPendingTestSuites: 0,
numTotalTests: 10,
numTotalTestSuites: 2,
snapshot: {},
startTime: Date.now() - 2000
};

Expand All @@ -181,6 +185,7 @@ describe('TapReporter', () => {
numPendingTestSuites: 1,
numTotalTests: 10,
numTotalTestSuites: 2,
snapshot: {},
startTime: Date.now() - 2000
};

Expand Down
7 changes: 7 additions & 0 deletions test/__snapshots__/LineWriter.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ exports[`LiveWriter .passed() colors "ok" green 1`] = `"{green ok}"`;

exports[`LiveWriter .pending() colors "ok" yellow 1`] = `"{yellow ok}"`;

exports[`LiveWriter .snapshots() when all values are greater than zero prints them all 1`] = `
Array [
"Snapshots",
"1 failed, 1 updated, 1 added, 1 passed, 4 total",
]
`;

exports[`LiveWriter .start() prints start message 1`] = `
Array [
Array [
Expand Down

0 comments on commit 6f30c01

Please sign in to comment.