Skip to content

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

fix(expect): support constructors in expect.arrayOf (#43126)

Pavel Feldmanb9dbe23ab142
diff --git a/packages/playwright/src/matchers/expectLibrary.ts b/packages/playwright/src/matchers/expectLibrary.ts
index 03af2f5975..659162ea6f 100644
--- a/packages/playwright/src/matchers/expectLibrary.ts
+++ b/packages/playwright/src/matchers/expectLibrary.ts
@@ -333,6 +333,10 @@ class ArrayContaining extends AsymmetricMatcher<Array<unknown>> {
}
class ArrayOf extends AsymmetricMatcher<unknown> {
+ constructor(sample: unknown, inverse = false) {
+ super(typeof sample === 'function' ? new Any(sample) : sample, inverse);
+ }
+
asymmetricMatch(other: unknown) {
const matcherContext = this.getMatcherContext();
const result =
diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts
index 25bf26e86b..a4984c91df 100644
--- a/tests/playwright-test/expect.spec.ts
+++ b/tests/playwright-test/expect.spec.ts
@@ -1341,6 +1341,28 @@ test('should support arrayOf', async ({ runInlineTest }) => {
expect(result.output).toContain('ArrayOf Any<Number>');
});
+test('should support arrayOf with constructor', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'expect-test.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ class Example {}
+ test('pass', () => {
+ expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));
+ expect([1, 2, 3]).toEqual(expect.arrayOf(Number));
+ expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));
+ expect(['a', 1]).toEqual(expect.not.arrayOf(String));
+ });
+ test('fail', () => {
+ expect(['a', 'b', 3]).toEqual(expect.arrayOf(String));
+ });
+ `
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.passed).toBe(1);
+ expect(result.failed).toBe(1);
+ expect(result.output).toContain('ArrayOf Any<String>');
+});
+
test('should account for undefined matcherResult', async ({ runInlineTest }) => {
const result = await runInlineTest({
'example.spec.ts': `