From 5b61c5d0eafc86c223673c43124797079ff9bfa1 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 5 Oct 2026 09:00:22 -0700 Subject: [PATCH] fix(expect): support constructors in expect.arrayOf Fixes: https://github.com/microsoft/playwright/issues/43117 --- .../playwright/src/matchers/expectLibrary.ts | 4 ++++ tests/playwright-test/expect.spec.ts | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packages/playwright/src/matchers/expectLibrary.ts b/packages/playwright/src/matchers/expectLibrary.ts index 03af2f597560d..659162ea6f058 100644 --- a/packages/playwright/src/matchers/expectLibrary.ts +++ b/packages/playwright/src/matchers/expectLibrary.ts @@ -333,6 +333,10 @@ class ArrayContaining extends AsymmetricMatcher> { } class ArrayOf extends AsymmetricMatcher { + 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 25bf26e86b382..a4984c91dfee4 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'); }); +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'); +}); + test('should account for undefined matcherResult', async ({ runInlineTest }) => { const result = await runInlineTest({ 'example.spec.ts': `