Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions packages/cli/test/snapshot-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -43,6 +44,7 @@ module.exports = {
*/
function initializeSnapshots(snapshotDir) {
let currentTest;
let snapshotErrors = false;

beforeEach(function setupSnapshots() {
// eslint-disable-next-line no-invalid-this
Expand All @@ -51,17 +53,34 @@ 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;
}
};
}

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) {
Expand All @@ -83,7 +102,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.',
);
}
Expand Down Expand Up @@ -153,7 +172,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.',
);
}
Expand Down Expand Up @@ -182,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) {
Expand Down