Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toMatchAriaSnapshot): fail test run when updating missing snapshot #34556

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions packages/playwright/src/matchers/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
const jestError = isJestError(e) ? e : null;
const error = jestError ? new ExpectError(jestError, customMessage, stackFrames) : e;
if (jestError?.matcherResult.suggestedRebaseline) {
// NOTE: this is a workaround for the fact that we can't pass the suggested rebaseline
// for passing matchers. See toMatchAriaSnapshot for a counterpart.
step.complete({ suggestedRebaseline: jestError?.matcherResult.suggestedRebaseline });
return;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/playwright/src/matchers/toMatchAriaSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ export async function toMatchAriaSnapshot(
return { pass: true, message: () => '', name: 'toMatchAriaSnapshot' };
} else {
const suggestedRebaseline = `\`\n${escapeTemplateString(indent(typedReceived.regex, '{indent} '))}\n{indent}\``;
if (updateSnapshots === 'missing') {
const message = 'A snapshot is not provided, generating new baseline.';
testInfo._hasNonRetriableError = true;
testInfo._failWithError(new Error(message));
}
// TODO: ideally, we should return "pass: true" here because this matcher passes
// when regenerating baselines. However, we can only access suggestedRebaseline in case
// of an error, so we fail here and workaround it in the expect implementation.
return { pass: false, message: () => '', name: 'toMatchAriaSnapshot', suggestedRebaseline };
}
}
Expand Down
17 changes: 9 additions & 8 deletions tests/playwright-test/update-aria-snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ test('should update missing snapshots', async ({ runInlineTest }, testInfo) => {
`
});

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

expect(stripAnsi(result.output).replace(/\\/g, '/')).toContain(`New baselines created for:

Expand Down Expand Up @@ -129,7 +130,7 @@ test('should update multiple missing snapshots', async ({ runInlineTest }, testI
`
});

expect(result.exitCode).toBe(0);
expect(result.exitCode).toBe(1);

expect(stripAnsi(result.output).replace(/\\/g, '/')).toContain(`New baselines created for:

Expand Down Expand Up @@ -188,7 +189,7 @@ test('should generate baseline with regex', async ({ runInlineTest }, testInfo)
`
});

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

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

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

expect(result.exitCode).toBe(0);
expect(result.exitCode).toBe(1);

expect(stripAnsi(result.output).replace(/\\/g, '/')).toContain(`New baselines created for:

Expand Down Expand Up @@ -430,7 +431,7 @@ test('should generate baseline for input values', async ({ runInlineTest }, test
`
});

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

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