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

This commit is contained in:
Max Schmitt 2024-07-17 11:37:02 +02:00
parent e11c0c0cbb
commit 75438d4966
3 changed files with 31 additions and 0 deletions

View file

@ -308,6 +308,8 @@ test('amount', async () => {
}); });
``` ```
### Compatibility with expect from Jest
:::note :::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`. 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, utils,
timeout: currentExpectTimeout() timeout: currentExpectTimeout()
}; };
(newThis as any).equals = throwUnsupportedExpectMatcherError;
return (matcher as any).call(newThis, ...args); return (matcher as any).call(newThis, ...args);
}; };
} }
@ -183,6 +184,10 @@ function createExpect(info: ExpectMetaInfo) {
return expectInstance; return expectInstance;
} }
function throwUnsupportedExpectMatcherError() {
throw new Error('Playwright Expect matchers are not fully compatible with Jest matchers. See https://aka.ms/playwright/expect-compatibility');
}
expectLibrary.setState({ expand: false }); expectLibrary.setState({ expand: false });
const customAsyncMatchers = { 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.failed).toBe(0);
expect(result.passed).toBe(2); 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('Playwright Expect matchers are not fully compatible with Jest matchers. See https://aka.ms/playwright/expect-compatibility');
});
`
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});