Skip to content

Commit 333c89a

Browse files
committed
chore: support arrayOf
1 parent fefc5cc commit 333c89a

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
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

docs/src/test-assertions-js.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Prefer [auto-retrying](#auto-retrying-assertions) assertions whenever possible.
9494
| [`method: GenericAssertions.any`] | Matches any instance of a class/primitive |
9595
| [`method: GenericAssertions.anything`] | Matches anything |
9696
| [`method: GenericAssertions.arrayContaining`] | Array contains specific elements |
97+
| [`method: GenericAssertions.arrayOf`] | Array contains elements of specific type |
9798
| [`method: GenericAssertions.closeTo`] | Number is approximately equal |
9899
| [`method: GenericAssertions.objectContaining`] | Object contains specific properties |
99100
| [`method: GenericAssertions.stringContaining`] | String contains a substring |

packages/playwright/types/test.d.ts

Lines changed: 22 additions & 0 deletions
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)

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/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ class TypesGenerator {
551551
'GenericAssertions.any',
552552
'GenericAssertions.anything',
553553
'GenericAssertions.arrayContaining',
554+
'GenericAssertions.arrayOf',
554555
'GenericAssertions.closeTo',
555556
'GenericAssertions.objectContaining',
556557
'GenericAssertions.stringContaining',

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)