Skip to content

Commit 09165a9

Browse files
committed
Fail snapshot test at done()
1 parent d105e2d commit 09165a9

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

packages/test/harness/src/host/common/host/done.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,12 @@ module.exports = (webDriver, resolve) =>
3535
expect(filteredErrors).toHaveLength(0);
3636
}
3737

38+
// All `toMatchImageSnapshot()` will pass so we can generate multiple diffs on a single run.
39+
// But always fail in `done()` when one of the snapshot test failed.
40+
// See /packages/test/harness/src/host/common/host/snapshot.js
41+
if ('__snapshotfail__' in webDriver) {
42+
throw new Error(webDriver.__snapshotfail__ || 'At least one of the snapshot test failed');
43+
}
44+
3845
resolve();
3946
};

packages/test/harness/src/host/common/host/snapshot.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@ module.exports = webDriver =>
1212

1313
const screenshot = await takeStabilizedScreenshot(webDriver);
1414

15-
expect(screenshot).toMatchImageSnapshot(
16-
mode === 'local'
17-
? {
18-
customDiffDir: testRoot,
19-
customSnapshotIdentifier: ({ counter, testPath }) => `${relative(testRoot, testPath)}.snap-${counter}`,
20-
customSnapshotsDir: testRoot
15+
try {
16+
expect(screenshot).toMatchImageSnapshot(
17+
mode === 'local'
18+
? {
19+
customDiffDir: testRoot,
20+
customSnapshotIdentifier: ({ counter, testPath }) => `${relative(testRoot, testPath)}.snap-${counter}`,
21+
customSnapshotsDir: testRoot
22+
}
23+
: {
24+
// jest-image-snapshot does not support <rootDir>.
25+
customSnapshotsDir: join(__dirname, '../../../../../../../__tests__/__image_snapshots__/html/')
26+
}
27+
);
28+
} catch (error) {
29+
// Hidden variable to fail the test.
30+
if (!('__snapshotfail__' in webDriver)) {
31+
Object.defineProperty(webDriver, '__snapshotfail__', {
32+
configurable: false,
33+
enumerable: true,
34+
get() {
35+
return error.message;
2136
}
22-
: {
23-
// jest-image-snapshot does not support <rootDir>.
24-
customSnapshotsDir: join(__dirname, '../../../../../../../__tests__/__image_snapshots__/html/')
25-
}
26-
);
37+
});
38+
}
39+
}
2740

2841
if (!options?.skipCheckAccessibility) {
2942
await checkAccessibilty(webDriver)();

0 commit comments

Comments
 (0)