Skip to content

Commit 159210d

Browse files
authored
cherry-pick(#34556): fix(toMatchAriaSnapshot): fail test run when updating missing snapshot (#34561)
1 parent fbad9f7 commit 159210d

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

packages/playwright/src/matchers/expect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
329329
const jestError = isJestError(e) ? e : null;
330330
const error = jestError ? new ExpectError(jestError, customMessage, stackFrames) : e;
331331
if (jestError?.matcherResult.suggestedRebaseline) {
332+
// NOTE: this is a workaround for the fact that we can't pass the suggested rebaseline
333+
// for passing matchers. See toMatchAriaSnapshot for a counterpart.
332334
step.complete({ suggestedRebaseline: jestError?.matcherResult.suggestedRebaseline });
333335
return;
334336
}

packages/playwright/src/matchers/toMatchAriaSnapshot.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ export async function toMatchAriaSnapshot(
140140
return { pass: true, message: () => '', name: 'toMatchAriaSnapshot' };
141141
} else {
142142
const suggestedRebaseline = `\`\n${escapeTemplateString(indent(typedReceived.regex, '{indent} '))}\n{indent}\``;
143+
if (updateSnapshots === 'missing') {
144+
const message = 'A snapshot is not provided, generating new baseline.';
145+
testInfo._hasNonRetriableError = true;
146+
testInfo._failWithError(new Error(message));
147+
}
148+
// TODO: ideally, we should return "pass: true" here because this matcher passes
149+
// when regenerating baselines. However, we can only access suggestedRebaseline in case
150+
// of an error, so we fail here and workaround it in the expect implementation.
143151
return { pass: false, message: () => '', name: 'toMatchAriaSnapshot', suggestedRebaseline };
144152
}
145153
}

tests/playwright-test/update-aria-snapshot.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ test('should update missing snapshots', async ({ runInlineTest }, testInfo) => {
8484
`
8585
});
8686

87-
expect(result.exitCode).toBe(0);
87+
expect(result.exitCode).toBe(1);
88+
expect(result.output).toContain('Error: A snapshot is not provided, generating new baseline.');
8889

8990
expect(stripAnsi(result.output).replace(/\\/g, '/')).toContain(`New baselines created for:
9091
@@ -129,7 +130,7 @@ test('should update multiple missing snapshots', async ({ runInlineTest }, testI
129130
`
130131
});
131132

132-
expect(result.exitCode).toBe(0);
133+
expect(result.exitCode).toBe(1);
133134

134135
expect(stripAnsi(result.output).replace(/\\/g, '/')).toContain(`New baselines created for:
135136
@@ -188,7 +189,7 @@ test('should generate baseline with regex', async ({ runInlineTest }, testInfo)
188189
`
189190
});
190191

191-
expect(result.exitCode).toBe(0);
192+
expect(result.exitCode).toBe(1);
192193
const patchPath = testInfo.outputPath('test-results/rebaselines.patch');
193194
const data = fs.readFileSync(patchPath, 'utf-8');
194195
expect(trimPatch(data)).toBe(`diff --git a/a.spec.ts b/a.spec.ts
@@ -249,7 +250,7 @@ test('should generate baseline with special characters', async ({ runInlineTest
249250
`
250251
});
251252

252-
expect(result.exitCode).toBe(0);
253+
expect(result.exitCode).toBe(1);
253254
const patchPath = testInfo.outputPath('test-results/rebaselines.patch');
254255
const data = fs.readFileSync(patchPath, 'utf-8');
255256
expect(trimPatch(data)).toBe(`diff --git a/a.spec.ts b/a.spec.ts
@@ -314,7 +315,7 @@ test('should update missing snapshots in tsx', async ({ runInlineTest }, testInf
314315
`,
315316
});
316317

317-
expect(result.exitCode).toBe(0);
318+
expect(result.exitCode).toBe(1);
318319
const patchPath = testInfo.outputPath('test-results/rebaselines.patch');
319320
const data = fs.readFileSync(patchPath, 'utf-8');
320321
expect(trimPatch(data)).toBe(`diff --git a/src/button.test.tsx b/src/button.test.tsx
@@ -370,7 +371,7 @@ test('should update multiple files', async ({ runInlineTest }, testInfo) => {
370371
`,
371372
});
372373

373-
expect(result.exitCode).toBe(0);
374+
expect(result.exitCode).toBe(1);
374375

375376
expect(stripAnsi(result.output).replace(/\\/g, '/')).toContain(`New baselines created for:
376377
@@ -430,7 +431,7 @@ test('should generate baseline for input values', async ({ runInlineTest }, test
430431
`
431432
});
432433

433-
expect(result.exitCode).toBe(0);
434+
expect(result.exitCode).toBe(1);
434435
const patchPath = testInfo.outputPath('test-results/rebaselines.patch');
435436
const data = fs.readFileSync(patchPath, 'utf-8');
436437
expect(trimPatch(data)).toBe(`diff --git a/a.spec.ts b/a.spec.ts
@@ -470,7 +471,7 @@ test('should update when options are specified', async ({ runInlineTest }, testI
470471
`
471472
});
472473

473-
expect(result.exitCode).toBe(0);
474+
expect(result.exitCode).toBe(1);
474475
const patchPath = testInfo.outputPath('test-results/rebaselines.patch');
475476
const data = fs.readFileSync(patchPath, 'utf-8');
476477
expect(trimPatch(data)).toBe(`diff --git a/a.spec.ts b/a.spec.ts

0 commit comments

Comments
 (0)