From e14934bfc030411b58fc3e3ba9759f1682244203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 20 Sep 2019 09:53:29 +0200 Subject: [PATCH 1/2] feat(cli): print help on updating snapshots when some snapshots were not matched MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- packages/cli/test/snapshot-matcher.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/cli/test/snapshot-matcher.js b/packages/cli/test/snapshot-matcher.js index cfa4d567b700..9468423b17a7 100644 --- a/packages/cli/test/snapshot-matcher.js +++ b/packages/cli/test/snapshot-matcher.js @@ -14,6 +14,7 @@ This is an initial experimental version. In the (near) future, we should move this file to a standalone package so that all Mocha users can use it. */ +const chalk = require('chalk'); const assert = require('assert'); const path = require('path'); @@ -43,6 +44,7 @@ module.exports = { */ function initializeSnapshots(snapshotDir) { let currentTest; + let snapshotErrors = false; beforeEach(function setupSnapshots() { // eslint-disable-next-line no-invalid-this @@ -51,8 +53,24 @@ function initializeSnapshots(snapshotDir) { }); if (!process.env.UPDATE_SNAPSHOTS) { + process.on('exit', function printSnapshotHelp() { + if (!snapshotErrors) return; + console.log( + chalk.red(` +Some of the snapshot-based tests have failed. Please carefully inspect +the differences to prevent possible regressions. If the changes are +intentional, run the tests again with \`UPDATE_SNAPSHOTS=1\` environment +variable to update snapshots. + `), + ); + }); return function expectToMatchSnapshot(actual) { - matchSnapshot(snapshotDir, currentTest, actual); + try { + matchSnapshot(snapshotDir, currentTest, actual); + } catch (err) { + snapshotErrors = true; + throw err; + } }; } @@ -83,7 +101,7 @@ function matchSnapshot(snapshotDir, currentTest, actualValue) { if (!(key in snapshotData)) { throw new Error( `No snapshot found for ${JSON.stringify(key)}.\n` + - 'Run the tests with UPDATE_SNAPSHOTS=1 in the environment ' + + 'Run the tests with `UPDATE_SNAPSHOTS=1` environment variable ' + 'to create and update snapshot files.', ); } @@ -153,7 +171,7 @@ function loadSnapshotData(snapshotFile) { if (err.code === 'MODULE_NOT_FOUND') { throw new Error( `Snapshot file ${snapshotFile} was not found.\n` + - 'Run the tests with UPDATE_SNAPSHOTS=1 in the environment ' + + 'Run the tests with `UPDATE_SNAPSHOTS=1` environment variable ' + 'to create and update snapshot files.', ); } From f3e754be2695cfcbbde11d570c00f69e9db347c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 20 Sep 2019 10:02:14 +0200 Subject: [PATCH 2/2] feat(cli): write snapshot files in parallel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- packages/cli/test/snapshot-matcher.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/cli/test/snapshot-matcher.js b/packages/cli/test/snapshot-matcher.js index 9468423b17a7..65167eb1960c 100644 --- a/packages/cli/test/snapshot-matcher.js +++ b/packages/cli/test/snapshot-matcher.js @@ -75,11 +75,12 @@ variable to update snapshots. } const snapshots = Object.create(null); - after(function updateSnapshots() { - for (const f in snapshots) { + after(async function updateSnapshots() { + const tasks = Object.entries(snapshots).map(([f, data]) => { const snapshotFile = buildSnapshotFilePath(snapshotDir, f); - writeSnapshotData(snapshotFile, snapshots[f]); - } + return writeSnapshotData(snapshotFile, data); + }); + await Promise.all(tasks); }); return function expectToRecordSnapshot(actual) { @@ -200,7 +201,7 @@ function writeSnapshotData(snapshotFile, snapshots) { const content = header + entries.join('\n'); mkdirp.sync(path.dirname(snapshotFile)); - writeFileAtomic.sync(snapshotFile, content, {encoding: 'utf-8'}); + return writeFileAtomic(snapshotFile, content, {encoding: 'utf-8'}); } function buildSnapshotCode(key, value) {