Skip to content

Commit c82288b

Browse files
committed
chore: support arrayOf
1 parent fefc5cc commit c82288b

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

docs/src/api/class-genericassertions.md

Lines changed: 26 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,31 @@ 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()]).toEqual(expect.arrayOf(Example));
640+
641+
// Match any number.
642+
expect([{ prop: 1 }]).toEqual({ prop: expect.arrayOf(Number) });
643+
644+
// Match any string.
645+
expect(['abc']).toEqual(expect.arrayOf(String));
646+
```
647+
648+
### param: GenericAssertions.arrayOf.constructor
649+
* since: v1.57
650+
- `constructor` <[Function]>
651+
652+
Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
653+
628654

629655
## method: GenericAssertions.closeTo
630656
* since: v1.9

packages/playwright/types/test.d.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7728,6 +7728,30 @@ 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()]).toEqual(expect.arrayOf(Example));
7744+
*
7745+
* // Match any number.
7746+
* expect([{ prop: 1 }]).toEqual({ prop: expect.arrayOf(Number) });
7747+
*
7748+
* // Match any string.
7749+
* expect(['abc']).toEqual(expect.arrayOf(String));
7750+
* ```
7751+
*
7752+
* @param constructor Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
7753+
*/
7754+
arrayOf(sample: unknown): AsymmetricMatcher;
77317755
/**
77327756
* Compares floating point numbers for approximate equality. Use this method inside
77337757
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
@@ -8111,6 +8135,7 @@ interface GenericAssertions<R> {
81118135
* - [expect(value).any(constructor)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-any)
81128136
* - [expect(value).anything()](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-anything)
81138137
* - [expect(value).arrayContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-containing)
8138+
* - [expect(value).arrayOf(constructor)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of)
81148139
* - [expect(value).closeTo(expected[, numDigits])](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-close-to)
81158140
* - [expect(value).objectContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-object-containing)
81168141
* - [expect(value).stringContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-string-containing)
@@ -8281,7 +8306,30 @@ interface GenericAssertions<R> {
82818306
* @param expected Expected error message or error object.
82828307
*/
82838308
toThrowError(error?: unknown): R;
8284-
8309+
/**
8310+
* `expect.arrayOf()` matches array of objects created from the
8311+
* [`constructor`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of-option-constructor)
8312+
* or a corresponding primitive type. Use it inside
8313+
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
8314+
* to perform pattern matching.
8315+
*
8316+
* **Usage**
8317+
*
8318+
* ```js
8319+
* // Match instance of a class.
8320+
* class Example {}
8321+
* expect([new Example()]).toEqual(expect.arrayOf(Example));
8322+
*
8323+
* // Match any number.
8324+
* expect([{ prop: 1 }]).toEqual({ prop: expect.arrayOf(Number) });
8325+
*
8326+
* // Match any string.
8327+
* expect(['abc']).toEqual(expect.arrayOf(String));
8328+
* ```
8329+
*
8330+
* @param constructor Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
8331+
*/
8332+
arrayOf(constructor: Function): void;
82858333
}
82868334

82878335
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)