Skip to content

Commit baf79a1

Browse files
committed
Update doc following finding requiring registering matcher with Jest
1 parent 3da5afe commit baf79a1

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

.npmrc

Whitespace-only changes.

check-node-version.js

Whitespace-only changes.

docs/Framework.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ We can pair `expect-webdriverio` with [Jest](https://jestjs.io/), [Mocha](https:
99
- 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.
1010

1111
### 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.
1414

1515
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).
1616
- 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
4747

4848

4949
#### 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.
5151

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`:
5269
```ts
5370
describe('My tests', async () => {
5471

@@ -65,7 +82,7 @@ Expected in `tsconfig.json`:
6582
"compilerOptions": {
6683
"types": [
6784
"@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`
6986
]
7087
}
7188
}

0 commit comments

Comments
 (0)