fix(expect): throw unsupported error when using this.equals() in expect (#31723)

This commit is contained in:
Max Schmitt 2024-07-17 13:22:00 +02:00 committed by GitHub
parent e11c0c0cbb
commit ed6abf86c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View File

@ -308,6 +308,8 @@ test('amount', async () => {
});
```
### Compatibility with expect library
:::note
Do not confuse Playwright's `expect` with the [`expect` library](https://jestjs.io/docs/expect). The latter is not fully integrated with Playwright test runner, so make sure to use Playwright's own `expect`.
:::

View File

@ -138,6 +138,7 @@ function createExpect(info: ExpectMetaInfo) {
utils,
timeout: currentExpectTimeout()
};
(newThis as any).equals = throwUnsupportedExpectMatcherError;
return (matcher as any).call(newThis, ...args);
};
}
@ -183,6 +184,10 @@ function createExpect(info: ExpectMetaInfo) {
return expectInstance;
}
function throwUnsupportedExpectMatcherError() {
throw new Error('It looks like you are using custom expect matchers that are not compatible with Playwright. See https://aka.ms/playwright/expect-compatibility');
}
expectLibrary.setState({ expand: false });
const customAsyncMatchers = {

View File

@ -1039,3 +1039,27 @@ test('should expose timeout to custom matchers', async ({ runInlineTest, runTSC
expect(result.failed).toBe(0);
expect(result.passed).toBe(2);
});
test('should throw error when using .equals()', async ({ runInlineTest }) => {
const result = await runInlineTest({
'helper.ts': `
import { test as base, expect } from '@playwright/test';
expect.extend({
toBeWithinRange(received, floor, ceiling) {
this.equals(1, 2);
},
});
export const test = base;
`,
'expect-test.spec.ts': `
import { test } from './helper';
test('numeric ranges', () => {
test.expect(() => {
test.expect(100).toBeWithinRange(90, 110);
}).toThrowError('It looks like you are using custom expect matchers that are not compatible with Playwright. See https://aka.ms/playwright/expect-compatibility');
});
`
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});