From 6e337413a74feafc1abe7cc8b68eff269570c19c Mon Sep 17 00:00:00 2001 From: Dirk-Jan Rutten Date: Wed, 24 May 2017 20:50:01 +0200 Subject: [PATCH] Improved the jest reporter with snapshot info per test. --- .../get_snapshot_status.test.js.snap | 22 +++++++ .../__tests__/default_reporter.test.js | 8 +++ .../__tests__/get_snapshot_status.test.js | 51 +++++++++++++++ .../src/reporters/default_reporter.js | 5 ++ .../src/reporters/get_snapshot_status.js | 64 +++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 packages/jest-cli/src/reporters/__tests__/__snapshots__/get_snapshot_status.test.js.snap create mode 100644 packages/jest-cli/src/reporters/__tests__/get_snapshot_status.test.js create mode 100644 packages/jest-cli/src/reporters/get_snapshot_status.js diff --git a/packages/jest-cli/src/reporters/__tests__/__snapshots__/get_snapshot_status.test.js.snap b/packages/jest-cli/src/reporters/__tests__/__snapshots__/get_snapshot_status.test.js.snap new file mode 100644 index 000000000000..e4344d6d9ca8 --- /dev/null +++ b/packages/jest-cli/src/reporters/__tests__/__snapshots__/get_snapshot_status.test.js.snap @@ -0,0 +1,22 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Retrieves the snapshot status 1`] = ` +Array [ + " › 1 snapshot written.", + " › 1 snapshot updated.", + " › 1 obsolete snapshot found.", + " › 1 snapshot test failed.", +] +`; + +exports[`Retrieves the snapshot status after a snapshot update 1`] = ` +Array [ + " › 2 snapshots written.", + " › 2 snapshots updated.", + " › 2 obsolete snapshots removed.", + " › Obsolete snapshot file removed.", + " › 2 snapshot tests failed.", +] +`; + +exports[`Shows no snapshot updates if all snapshots matched 1`] = `Array []`; diff --git a/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js b/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js index d22815366861..45710329751f 100644 --- a/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js +++ b/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js @@ -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', }; diff --git a/packages/jest-cli/src/reporters/__tests__/get_snapshot_status.test.js b/packages/jest-cli/src/reporters/__tests__/get_snapshot_status.test.js new file mode 100644 index 000000000000..e9ba8053db20 --- /dev/null +++ b/packages/jest-cli/src/reporters/__tests__/get_snapshot_status.test.js @@ -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(); +}); diff --git a/packages/jest-cli/src/reporters/default_reporter.js b/packages/jest-cli/src/reporters/default_reporter.js index 08d4a9f24750..84eb33eb6258 100644 --- a/packages/jest-cli/src/reporters/default_reporter.js +++ b/packages/jest-cli/src/reporters/default_reporter.js @@ -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; @@ -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); } } } diff --git a/packages/jest-cli/src/reporters/get_snapshot_status.js b/packages/jest-cli/src/reporters/get_snapshot_status.js new file mode 100644 index 000000000000..ebd7f34b9f5c --- /dev/null +++ b/packages/jest-cli/src/reporters/get_snapshot_status.js @@ -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, + afterUpdate: boolean, +): Array => { + 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; +};