You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Framework.md
+21-4Lines changed: 21 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,8 @@ We can pair `expect-webdriverio` with [Jest](https://jestjs.io/), [Mocha](https:
9
9
- When `expect` is defined globally, we usually overwrite it with the one from `expect-webdriverio` to have our defined assertions work out of the box.
10
10
11
11
### Jest
12
-
We can use `expect-webdriverio` with [Jest](https://jestjs.io/) using either [`@jest/globals`](https://www.npmjs.com/package/@jest/globals) (preferred) or [`@types/jest`](https://www.npmjs.com/package/@types/jest) (has global imports support).
13
-
- Note: Jest maintainers do not support [`@types/jest`](https://www.npmjs.com/package/@types/jest). In case this library gets out of date or has problems, support might be dropped.
12
+
We can use `expect-webdriverio` with [Jest](https://jestjs.io/) using either [`@jest/globals`](https://www.npmjs.com/package/@jest/globals) (preferred) or [`@types/jest`](https://www.npmjs.com/package/@types/jest) (which has global imports support).
13
+
- Note: Jest maintainers do not support [`@types/jest`](https://www.npmjs.com/package/@types/jest). If this library gets out of date or has problems, support might be dropped.
14
14
15
15
In each case, when used <u>**outside of [WDIO Testrunner](https://webdriver.io/docs/clioptions)**</u>, types need to be added to your [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html).
16
16
- Note: With Jest, the matchers `toMatchSnapshot` and `toMatchInlineSnapshot` are overloaded. To resolve the types correctly, `expect-webdriverio/jest` must be last.
@@ -47,8 +47,25 @@ This [Jest issue](https://github.com/jestjs/jest/issues/12424) seems to target t
47
47
48
48
49
49
#### With `@types/jest`
50
-
When paired with [Jest](https://jestjs.io/) and [`@types/jest`](https://www.npmjs.com/package/@types/jest), no imports are required. Global types are already defined correctly.
50
+
When also paired with [`@types/jest`](https://www.npmjs.com/package/@types/jest), no imports are required. Global types are already defined correctly and you can simply use Jest's `expect` directly.
51
51
52
+
Optional: If you are NOT using WDIO Testrunner, it might be required to correctly register the WDIO matchers on Jest's `expect` as shown below:
53
+
```ts
54
+
import { matchers } from"expect-webdriverio";
55
+
56
+
// Import and extend Jest's expect with WebdriverIO matchers
57
+
beforeAll(async () => {
58
+
// Convert the Map to a plain object and extend Jest's expect
59
+
const matchersObject:Record<string, any> = {};
60
+
matchers.forEach((matcher, name) => {
61
+
matchersObject[name] =matcher;
62
+
});
63
+
64
+
expect.extend(matchersObject);
65
+
});
66
+
```
67
+
68
+
As shown below, no imports are required and we can use WDIO matchers directly on Jest's `expect`:
52
69
```ts
53
70
describe('My tests', async () => {
54
71
@@ -65,7 +82,7 @@ Expected in `tsconfig.json`:
65
82
"compilerOptions": {
66
83
"types": [
67
84
"@types/jest",
68
-
"expect-webdriverio/jest"// Must be after for overloaded matchers `toMatchSnapshot` and `toMatchInlineSnapshot`
85
+
"expect-webdriverio/jest"// Must be last for overloaded matchers `toMatchSnapshot` and `toMatchInlineSnapshot`
0 commit comments