Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/src/api/class-genericassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ expect(value).toEqual({ prop: 1 });
* [`method: GenericAssertions.any`]
* [`method: GenericAssertions.anything`]
* [`method: GenericAssertions.arrayContaining`]
* [`method: GenericAssertions.arrayOf`]
* [`method: GenericAssertions.closeTo`]
* [`method: GenericAssertions.objectContaining`]
* [`method: GenericAssertions.stringContaining`]
Expand Down Expand Up @@ -625,6 +626,28 @@ expect([1, 2, 3]).not.toEqual(expect.arrayContaining([1, 4]));
Expected array that is a subset of the received value.


## method: GenericAssertions.arrayOf
* since: v1.57

`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.

**Usage**

```js
// Match instance of a class.
class Example {}
expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));

// Match any string.
expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));
```

### param: GenericAssertions.arrayOf.constructor
* since: v1.57
- `constructor` <[Function]>

Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.


## method: GenericAssertions.closeTo
* since: v1.9
Expand Down
1 change: 1 addition & 0 deletions docs/src/test-assertions-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Prefer [auto-retrying](#auto-retrying-assertions) assertions whenever possible.
| [`method: GenericAssertions.any`] | Matches any instance of a class/primitive |
| [`method: GenericAssertions.anything`] | Matches anything |
| [`method: GenericAssertions.arrayContaining`] | Array contains specific elements |
| [`method: GenericAssertions.arrayOf`] | Array contains elements of specific type |
| [`method: GenericAssertions.closeTo`] | Number is approximately equal |
| [`method: GenericAssertions.objectContaining`] | Object contains specific properties |
| [`method: GenericAssertions.stringContaining`] | String contains a substring |
Expand Down
22 changes: 22 additions & 0 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7728,6 +7728,27 @@ interface AsymmetricMatchers {
* @param expected Expected array that is a subset of the received value.
*/
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
/**
* `expect.arrayOf()` matches array of objects created from the
* [`constructor`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of-option-constructor)
* or a corresponding primitive type. Use it inside
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
* to perform pattern matching.
*
* **Usage**
*
* ```js
* // Match instance of a class.
* class Example {}
* expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));
*
* // Match any string.
* expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));
* ```
*
* @param constructor Constructor of the expected object like `ExampleClass`, or a primitive boxed type like `Number`.
*/
arrayOf(sample: unknown): AsymmetricMatcher;
/**
* Compares floating point numbers for approximate equality. Use this method inside
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal)
Expand Down Expand Up @@ -8111,6 +8132,7 @@ interface GenericAssertions<R> {
* - [expect(value).any(constructor)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-any)
* - [expect(value).anything()](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-anything)
* - [expect(value).arrayContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-containing)
* - [expect(value).arrayOf(constructor)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-array-of)
* - [expect(value).closeTo(expected[, numDigits])](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-close-to)
* - [expect(value).objectContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-object-containing)
* - [expect(value).stringContaining(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-string-containing)
Expand Down
18 changes: 18 additions & 0 deletions tests/playwright-test/expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1301,3 +1301,21 @@ test('multiple custom asymmetric matchers in async expect should present the cor
expect(result.output).toContain('- \"aProperty\": isUndefined<>');
expect(result.output).toContain('+ \"aProperty\": \"foo\"');
});

test('should support arrayOf', async ({ runInlineTest }) => {
const result = await runInlineTest({
'expect-test.spec.ts': `
import { test, expect } from '@playwright/test';
test('pass', () => {
expect([1,2,3]).toEqual(expect.arrayOf(expect.any(Number)));
});
test('fail', () => {
expect([1,2,'3']).toEqual(expect.arrayOf(expect.any(Number)));
});
`
});
expect(result.exitCode).toBe(1);
expect(result.passed).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('ArrayOf Any<Number>');
});
1 change: 1 addition & 0 deletions utils/generate_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ class TypesGenerator {
'GenericAssertions.any',
'GenericAssertions.anything',
'GenericAssertions.arrayContaining',
'GenericAssertions.arrayOf',
'GenericAssertions.closeTo',
'GenericAssertions.objectContaining',
'GenericAssertions.stringContaining',
Expand Down
1 change: 1 addition & 0 deletions utils/generate_types/overrides-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ interface AsymmetricMatchers {
any(sample: unknown): AsymmetricMatcher;
anything(): AsymmetricMatcher;
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
arrayOf(sample: unknown): AsymmetricMatcher;
closeTo(sample: number, precision?: number): AsymmetricMatcher;
objectContaining(sample: Record<string, unknown>): AsymmetricMatcher;
stringContaining(sample: string): AsymmetricMatcher;
Expand Down
Loading