Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(expect): do not attach `-expected` snapshot when `updateShapshots: "none"` (#41474)

Devin Roussoe2a3f487c43c
diff --git a/packages/playwright/src/matchers/toMatchSnapshot.ts b/packages/playwright/src/matchers/toMatchSnapshot.ts
index e075b65f98..d835b1302a 100644
--- a/packages/playwright/src/matchers/toMatchSnapshot.ts
+++ b/packages/playwright/src/matchers/toMatchSnapshot.ts
@@ -187,9 +187,10 @@ class SnapshotHelper {
handleMissing(actual: Buffer | string): MatcherResult<string, string> {
const attachments: MatcherAttachment[] = [];
const isWriteMissingMode = this.updateSnapshots !== 'none';
- if (isWriteMissingMode)
+ if (isWriteMissingMode) {
writeFileSync(this.expectedPath, actual);
- attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-expected'), contentType: this.mimeType, path: this.expectedPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-expected'), contentType: this.mimeType, path: this.expectedPath });
+ }
writeFileSync(this.actualPath, actual);
attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-actual'), contentType: this.mimeType, path: this.actualPath });
const message = `A snapshot doesn't exist at ${this.expectedPath}${isWriteMissingMode ? ', writing actual.' : '.'}`;
diff --git a/tests/playwright-test/golden.spec.ts b/tests/playwright-test/golden.spec.ts
index 57dac49be8..29444d6384 100644
--- a/tests/playwright-test/golden.spec.ts
+++ b/tests/playwright-test/golden.spec.ts
@@ -161,6 +161,25 @@ test('should generate separate actual results for repeating names', async ({ run
]);
});
+test('should not attach a missing expected snapshot when update-snapshots is none', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ ...files,
+ 'a.spec.js': `
+ const { test, expect } = require('./helper');
+ test.afterEach(async ({}, testInfo) => {
+ console.log('## ' + JSON.stringify(testInfo.attachments.map(a => a.name)));
+ });
+ test('is a test', ({}) => {
+ expect.soft('a').toMatchSnapshot('foo.txt');
+ });
+ `
+ }, { 'update-snapshots': 'none' });
+ expect(result.exitCode).toBe(1);
+ const names = result.output.split('\n').filter(l => l.startsWith('## ')).map(l => JSON.parse(l.substring(3)))[0];
+ expect(names).toContain('foo-actual.txt');
+ expect(names).not.toContain('foo-expected.txt');
+});
+
test('should compile with different option combinations', async ({ runTSC }) => {
const result = await runTSC({
'a.spec.ts': `