Skip to content

Commit 134862f

Browse files
committed
chore: support arrayOf
1 parent fefc5cc commit 134862f

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

docs/src/api/class-genericassertions.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ expect(value).toEqual({ prop: 1 });
354354
* [`method: GenericAssertions.any`]
355355
* [`method: GenericAssertions.anything`]
356356
* [`method: GenericAssertions.arrayContaining`]
357+
* [`method: GenericAssertions.arrayOf`]
357358
* [`method: GenericAssertions.closeTo`]
358359
* [`method: GenericAssertions.objectContaining`]
359360
* [`method: GenericAssertions.stringContaining`]
@@ -625,6 +626,28 @@ expect([1, 2, 3]).not.toEqual(expect.arrayContaining([1, 4]));
625626
Expected array that is a subset of the received value.
626627

627628

629+
## method: GenericAssertions.arrayOf
630+
* since: v1.57
631+
632+
`expect.arrayOf()` matches array of objects created from the [`param: constructor`] or a corresponding primitive type. Use it inside [`method: GenericAssertions.toEqual`] to perform pattern matching.
633+
634+
**Usage**
635+
636+
```js
637+
// Match instance of a class.
638+
class Example {}
639+
expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));
640+
641+
// Match any string.
642+
expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));
643+
```
644+
645+
### param: GenericAssertions.arrayOf.constructor
646+
* since: v1.57
647+
- `constructor` <[Function]>
648+
649+
Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
650+
628651

629652
## method: GenericAssertions.closeTo
630653
* since: v1.9

packages/playwright/types/test.d.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7728,6 +7728,27 @@ interface AsymmetricMatchers {
77287728
* @param expected Expected array that is a subset of the received value.
77297729
*/
77307730
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
7731+
/**
7732+
* `expect.arrayOf()` matches array of objects created from the
7733+
* [`constructor`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of-option-constructor)
7734+
* or a corresponding primitive type. Use it inside
7735+
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
7736+
* to perform pattern matching.
7737+
*
7738+
* **Usage**
7739+
*
7740+
* ```js
7741+
* // Match instance of a class.
7742+
* class Example {}
7743+
* expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));
7744+
*
7745+
* // Match any string.
7746+
* expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));
7747+
* ```
7748+
*
7749+
* @param constructor Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
7750+
*/
7751+
arrayOf(sample: unknown): AsymmetricMatcher;
77317752
/**
77327753
* Compares floating point numbers for approximate equality. Use this method inside
77337754
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
@@ -8111,6 +8132,7 @@ interface GenericAssertions<R> {
81118132
* - [expect(value).any(constructor)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-any)
81128133
* - [expect(value).anything()](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-anything)
81138134
* - [expect(value).arrayContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-containing)
8135+
* - [expect(value).arrayOf(constructor)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of)
81148136
* - [expect(value).closeTo(expected[, numDigits])](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-close-to)
81158137
* - [expect(value).objectContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-object-containing)
81168138
* - [expect(value).stringContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-string-containing)
@@ -8281,7 +8303,27 @@ interface GenericAssertions<R> {
82818303
* @param expected Expected error message or error object.
82828304
*/
82838305
toThrowError(error?: unknown): R;
8284-
8306+
/**
8307+
* `expect.arrayOf()` matches array of objects created from the
8308+
* [`constructor`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of-option-constructor)
8309+
* or a corresponding primitive type. Use it inside
8310+
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
8311+
* to perform pattern matching.
8312+
*
8313+
* **Usage**
8314+
*
8315+
* ```js
8316+
* // Match instance of a class.
8317+
* class Example {}
8318+
* expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));
8319+
*
8320+
* // Match any string.
8321+
* expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));
8322+
* ```
8323+
*
8324+
* @param constructor Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
8325+
*/
8326+
arrayOf(constructor: Function): void;
82858327
}
82868328

82878329
type FunctionAssertions = {

tests/playwright-test/expect.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,3 +1301,21 @@ test('multiple custom asymmetric matchers in async expect should present the cor
13011301
expect(result.output).toContain('- \"aProperty\": isUndefined<>');
13021302
expect(result.output).toContain('+ \"aProperty\": \"foo\"');
13031303
});
1304+
1305+
test('should support arrayOf', async ({ runInlineTest }) => {
1306+
const result = await runInlineTest({
1307+
'expect-test.spec.ts': `
1308+
import { test, expect } from '@playwright/test';
1309+
test('pass', () => {
1310+
expect([1,2,3]).toEqual(expect.arrayOf(expect.any(Number)));
1311+
});
1312+
test('fail', () => {
1313+
expect([1,2,'3']).toEqual(expect.arrayOf(expect.any(Number)));
1314+
});
1315+
`
1316+
});
1317+
expect(result.exitCode).toBe(1);
1318+
expect(result.passed).toBe(1);
1319+
expect(result.failed).toBe(1);
1320+
expect(result.output).toContain('ArrayOf Any<Number>');
1321+
});

utils/generate_types/overrides-test.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ interface AsymmetricMatchers {
321321
any(sample: unknown): AsymmetricMatcher;
322322
anything(): AsymmetricMatcher;
323323
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
324+
arrayOf(sample: unknown): AsymmetricMatcher;
324325
closeTo(sample: number, precision?: number): AsymmetricMatcher;
325326
objectContaining(sample: Record<string, unknown>): AsymmetricMatcher;
326327
stringContaining(sample: string): AsymmetricMatcher;

0 commit comments

Comments
 (0)