Skip to content

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

chore: throw when a Promise was passed to toMatchSnapshot (#12906)

Max Schmittd8ab76bf64b8
diff --git a/packages/playwright-test/src/matchers/toMatchSnapshot.ts b/packages/playwright-test/src/matchers/toMatchSnapshot.ts
index 5fb1e38c43..3bf5622794 100644
--- a/packages/playwright-test/src/matchers/toMatchSnapshot.ts
+++ b/packages/playwright-test/src/matchers/toMatchSnapshot.ts
@@ -243,6 +243,8 @@ export function toMatchSnapshot(
const testInfo = currentTestInfo();
if (!testInfo)
throw new Error(`toMatchSnapshot() must be called during the test`);
+ if (received instanceof Promise)
+ throw new Error('An unresolved Promise was passed to toMatchSnapshot(), make sure to resolve it by adding await to it.');
const helper = new SnapshotHelper(
testInfo, testInfo.snapshotPath.bind(testInfo), determineFileExtension(received),
testInfo.project.expect?.toMatchSnapshot || {},
diff --git a/tests/playwright-test/golden.spec.ts b/tests/playwright-test/golden.spec.ts
index 33a98144b3..86681c024a 100644
--- a/tests/playwright-test/golden.spec.ts
+++ b/tests/playwright-test/golden.spec.ts
@@ -922,3 +922,17 @@ test('should allow comparing text with text without file extension', async ({ ru
});
expect(result.exitCode).toBe(0);
});
+
+test('should throw if a Promise was passed to toMatchSnapshot', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ ...files,
+ 'a.spec.js': `
+ const { test } = require('./helper');
+ test('is a test', ({}) => {
+ expect(() => expect(new Promise(() => {})).toMatchSnapshot('foobar')).toThrow(/An unresolved Promise was passed to toMatchSnapshot\\(\\), make sure to resolve it by adding await to it./);
+ });
+ `
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});