Skip to content

Commit

Permalink
Improved the jest reporter with snapshot info per test.
Browse files Browse the repository at this point in the history
  • Loading branch information
excitement-engineer committed Aug 26, 2017
1 parent db4465e commit 6e33741
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Retrieves the snapshot status 1`] = `
Array [
"<bold><green> › 1 snapshot</> written.",
"<bold><green> › 1 snapshot</> updated.",
"<bold><red> › 1 obsolete snapshot</> found.",
"<bold><red> › 1 snapshot test</> failed.",
]
`;
exports[`Retrieves the snapshot status after a snapshot update 1`] = `
Array [
"<bold><green> › 2 snapshots</> written.",
"<bold><green> › 2 snapshots</> updated.",
"<bold><red> › 2 obsolete snapshots</> removed.",
"<bold><red> › Obsolete snapshot file</> removed.",
"<bold><red> › 2 snapshot tests</> failed.",
]
`;
exports[`Shows no snapshot updates if all snapshots matched 1`] = `Array []`;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const testCase = {
path: '/foo',
};
const testResult = {
snapshot: {
added: 0,
fileDeleted: true,
matched: 1,
unchecked: 0,
unmatched: 0,
updated: 0,
},
testFilePath: '/foo',
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

const getSnapshotStatus = require('../get_snapshot_status');

test('Retrieves the snapshot status', () => {
const snapshotResult = {
added: 1,
fileDeleted: false,
matched: 1,
unchecked: 1,
unmatched: 1,
updated: 1,
};

expect(getSnapshotStatus(snapshotResult, false)).toMatchSnapshot();
});

test('Shows no snapshot updates if all snapshots matched', () => {
const snapshotResult = {
added: 0,
fileDeleted: false,
matched: 1,
unchecked: 0,
unmatched: 0,
updated: 0,
};

expect(getSnapshotStatus(snapshotResult, true)).toMatchSnapshot();
});

test('Retrieves the snapshot status after a snapshot update', () => {
const snapshotResult = {
added: 2,
fileDeleted: true,
matched: 2,
unchecked: 2,
unmatched: 2,
updated: 2,
};

expect(getSnapshotStatus(snapshotResult, true)).toMatchSnapshot();
});
5 changes: 5 additions & 0 deletions packages/jest-cli/src/reporters/default_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import isCI from 'is-ci';
import BaseReporter from './base_reporter';
import Status from './Status';
import getResultHeader from './get_result_header';
import getSnapshotStatus from './get_snapshot_status';

type write = (chunk: string, enc?: any, cb?: () => void) => boolean;
type FlushBufferedOutput = () => void;
Expand Down Expand Up @@ -192,6 +193,10 @@ class DefaultReporter extends BaseReporter {
if (result.failureMessage) {
this.log(result.failureMessage);
}

const didUpdate = this._globalConfig.updateSnapshot === 'all';
const snapshotStatuses = getSnapshotStatus(result.snapshot, didUpdate);
snapshotStatuses.forEach(this.log);
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions packages/jest-cli/src/reporters/get_snapshot_status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

import type {TestResult} from 'types/TestResult';

const chalk = require('chalk');

const {pluralize} = require('./utils');

const ARROW = ' \u203A ';
const FAIL_COLOR = chalk.bold.red;
const SNAPSHOT_ADDED = chalk.bold.green;
const SNAPSHOT_REMOVED = chalk.bold.red;
const SNAPSHOT_UPDATED = chalk.bold.green;

module.exports = (
snapshot: $PropertyType<TestResult, 'snapshot'>,
afterUpdate: boolean,
): Array<string> => {
const statuses = [];

if (snapshot.added) {
statuses.push(
SNAPSHOT_ADDED(ARROW + pluralize('snapshot', snapshot.added)) +
' written.',
);
}

if (snapshot.updated) {
statuses.push(
SNAPSHOT_UPDATED(ARROW + pluralize('snapshot', snapshot.updated)) +
` updated.`,
);
}

if (snapshot.unchecked) {
statuses.push(
FAIL_COLOR(ARROW + pluralize('obsolete snapshot', snapshot.unchecked)) +
(afterUpdate ? ' removed' : ' found') +
'.',
);
}

if (snapshot.fileDeleted) {
statuses.push(
SNAPSHOT_REMOVED(ARROW + 'Obsolete snapshot file') + ` removed.`,
);
}

if (snapshot.unmatched) {
statuses.push(
FAIL_COLOR(ARROW + pluralize('snapshot test', snapshot.unmatched)) +
' failed.',
);
}
return statuses;
};

0 comments on commit 6e33741

Please sign in to comment.