@@ -8,46 +8,68 @@ import LiteYouTube from '@site/src/components/LiteYouTube';
8
8
9
9
## Version 1.39
10
10
11
- ### Extending expect with custom matchers
11
+ ### Add custom matchers to your expect
12
12
13
13
You can extend Playwright assertions by providing custom matchers. These matchers will be available on the expect object.
14
14
15
- ``` js title=fixtures.ts
15
+ ``` js title="test.spec.ts"
16
16
import { expect as baseExpect } from ' @playwright/test' ;
17
17
export const expect = baseExpect .extend ({
18
18
async toHaveAmount (locator : Locator , expected : number , options ?: { timeout?: number }) {
19
- // Note: this matcher never passes, see the documentation for a full example.
20
- // Return a "pass" flag and a message getter.
21
- return { pass: false , message : () => ` Expected ${ expected} amount` };
19
+ // ... see documentation for how to write matchers.
22
20
},
23
21
});
22
+
23
+ test (' pass' , async ({ page }) => {
24
+ await expect (page .getByTestId (' cart' )).toHaveAmount (5 );
25
+ });
24
26
```
25
27
26
28
See the documentation [ for a full example] ( ./test-configuration.md#add-custom-matchers-using-expectextend ) .
27
29
28
- ### Merging fixtures and expect matchers
30
+ ### Merge test fixtures
31
+
32
+ You can now merge test fixtures from multiple files or modules:
33
+
34
+ ``` js title="fixtures.ts"
35
+ import { mergeTests } from ' @playwright/test' ;
36
+ import { test as dbTest } from ' database-test-utils' ;
37
+ import { test as a11yTest } from ' a11y-test-utils' ;
38
+
39
+ export const test = mergeTests (dbTest, a11yTest);
40
+ ```
41
+
42
+ ``` js title="test.spec.ts"
43
+ import { test } from ' ./fixtures' ;
44
+
45
+ test (' passes' , async ({ database, page, a11y }) => {
46
+ // use database and a11y fixtures.
47
+ });
48
+ ```
49
+
50
+ ### Merge custom expect matchers
29
51
30
- You can combine fixtures and custom expect matchers from multiple files or modules.
52
+ You can now merge custom expect matchers from multiple files or modules:
31
53
32
54
``` js title="fixtures.ts"
33
- import { composedTest , composedExpect } from ' @playwright/test' ;
55
+ import { mergeTests , mergeExpects } from ' @playwright/test' ;
34
56
import { test as dbTest , expect as dbExpect } from ' database-test-utils' ;
35
57
import { test as a11yTest , expect as a11yExpect } from ' a11y-test-utils' ;
36
58
37
- export const expect = composedExpect (dbExpect, a11yExpect );
38
- export const test = composedTest (dbTest, a11yTest );
59
+ export const test = mergeTests (dbTest, a11yTest );
60
+ export const expect = mergeExpects (dbExpect, a11yExpect );
39
61
```
40
62
41
63
``` js title="test.spec.ts"
42
64
import { test , expect } from ' ./fixtures' ;
43
65
44
- test (' passes' , async ({ database, page }) => {
66
+ test (' passes' , async ({ page, database }) => {
45
67
await expect (database).toHaveDatabaseUser (' admin' );
46
68
await expect (page).toPassA11yAudit ();
47
69
});
48
70
```
49
71
50
- ### Boxed test steps
72
+ ### Hide implementation details: box test steps
51
73
52
74
You can mark a [ ` method: Test.step ` ] as "boxed" so that errors inside it point to the step call site.
53
75
0 commit comments