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

This commit is contained in:
Max Schmitt 2022-03-22 16:36:09 +01:00 committed by GitHub
parent 4b877213a1
commit d8ab76bf64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View file

@ -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 || {},

View file

@ -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);
});