Skip to content

Commit 3da5afe

Browse files
committed
Add finding on augmentation of @jest/globals
1 parent a137555 commit 3da5afe

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

docs/Framework.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# Expect-WebDriverIO Framework
22

3-
Expect-WebDriverIO is inspired by [`expect`](https://www.npmjs.com/package/expect) but also extending it. Therefore we can exploit usually everything provided by the API of expect with some WebDriverIO touch.
3+
Expect-WebDriverIO is inspired by [`expect`](https://www.npmjs.com/package/expect) but also extends it. Therefore, we can use everything provided by the expect API with some WebDriverIO enhancements.
44
- Note: Yes, this is a package of Jest but it is usable without Jest.
55

66
## Compatibility
77

8-
We can pair `expect-webdriverio` with [Jest](https://jestjs.io/), [Mocha](https://mochajs.org/), [Jasmine](https://jasmine.github.io/).
9-
- When an `expect` is defined globally, we usually overwrite it with the one of `expect-webdriverio` to have our defined assertions work out of the box.
8+
We can pair `expect-webdriverio` with [Jest](https://jestjs.io/), [Mocha](https://mochajs.org/), and [Jasmine](https://jasmine.github.io/).
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.
1010

1111
### Jest
12-
We can use `expect-webdriverio` with [Jest](https://jestjs.io/) with either the [`@jest/globals`](https://www.npmjs.com/package/@jest/globals) (preferred) or the [`@types/jest`](https://www.npmjs.com/package/@types/jest) (has global imports support)
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).
1313
- 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.
1414

15-
In each case, when used <u>**outside of [WDIO Testrunner](https://webdriver.io/docs/clioptions)**</u>, types are required to be added in your [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
16-
- Note: With Jest the matchers `toMatchSnapshot` and `toMatchInlineSnapshot` were overloaded. To resolve correctly the types `expect-webdriverio/jest` must be last.
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+
- Note: With Jest, the matchers `toMatchSnapshot` and `toMatchInlineSnapshot` are overloaded. To resolve the types correctly, `expect-webdriverio/jest` must be last.
1717

1818
#### With `@jest/globals`
19-
When paired with [Jest](https://jestjs.io/) and the [`@jest/globals`](https://www.npmjs.com/package/@jest/globals), we should `import` the `expect` keyword from `expect-webdriverio`
19+
When paired with [Jest](https://jestjs.io/) and [`@jest/globals`](https://www.npmjs.com/package/@jest/globals), we should `import` the `expect` function from `expect-webdriverio`.
2020

2121
```ts
2222
import { expect } from 'expect-webdriverio'
@@ -31,18 +31,23 @@ describe('My tests', async () => {
3131
})
3232
```
3333

34-
No `types` is expected in `tsconfig.json`
35-
Optionally, to not need `import { expect } from 'expect-webdriverio'` you can use the below
34+
No `types` are expected in `tsconfig.json`.
35+
Optionally, to avoid needing `import { expect } from 'expect-webdriverio'`, you can use the following:
3636
```json
3737
{
3838
"compilerOptions": {
3939
"types": ["expect-webdriverio/expect-global"]
4040
}
4141
}
42-
```
42+
```
43+
##### Augmenting Jest's `expect`
44+
Multiple attempts were made to augment `@jest/globals` to support `expect-webdriverio` matchers directly on Jest's `expect`. However, no namespace is available to augment it; therefore, only module augmentation can be used. This method does not allow adding matchers with the `extends` keyword; instead, they need to be added directly in the interface of the module declaration augmentation, which would create a lot of code duplication.
45+
46+
This [Jest issue](https://github.com/jestjs/jest/issues/12424) seems to target this problem, but it is still in progress.
47+
4348

4449
#### With `@types/jest`
45-
When paired with [Jest](https://jestjs.io/) and the [`@types/jest`](https://www.npmjs.com/package/@types/jest), no imports are required. Global ones are already defined correctly
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.
4651

4752
```ts
4853
describe('My tests', async () => {
@@ -67,10 +72,10 @@ Expected in `tsconfig.json`:
6772
```
6873

6974
### Mocha
70-
When paired with [Mocha](https://mochajs.org/), it can be used without (standalone) or with [`chai`](https://www.chaijs.com/) (or any other assertion library)
75+
When paired with [Mocha](https://mochajs.org/), it can be used without (standalone) or with [`chai`](https://www.chaijs.com/) (or any other assertion library).
7176

7277
#### Standalone
73-
No import is required, everything is set globally
78+
No import is required; everything is set globally.
7479

7580
```ts
7681
describe('My tests', async () => {
@@ -95,16 +100,16 @@ Expected in `tsconfig.json`:
95100
```
96101

97102
#### Chai
98-
`expect-webdriverio` can coexist with [Chai](https://www.chaijs.com/) assertion library, by importing both library explicitly.
99-
See also this [documentation](https://webdriver.io/docs/assertion/#migrating-from-chai)
103+
`expect-webdriverio` can coexist with the [Chai](https://www.chaijs.com/) assertion library by importing both libraries explicitly.
104+
See also this [documentation](https://webdriver.io/docs/assertion/#migrating-from-chai).
100105

101106
### Jasmine
102-
When paired with [Jasmine](https://jasmine.github.io/), [`@wdio/jasmine-framework`](https://www.npmjs.com/package/@wdio/jasmine-framework) is also required to have it configured correctly as it needs to force the `expect` to be `expectAsync` and also to register the wdio matchers with `addAsyncMatcher` since `expect-webdriverio` only supports the jest style `expect.extend` version.
107+
When paired with [Jasmine](https://jasmine.github.io/), [`@wdio/jasmine-framework`](https://www.npmjs.com/package/@wdio/jasmine-framework) is also required to configure it correctly, as it needs to force `expect` to be `expectAsync` and also register the WDIO matchers with `addAsyncMatcher` since `expect-webdriverio` only supports the Jest-style `expect.extend` version.
103108

104-
The types `expect-webdriverio/jasmine` is still offers but subject to removal or to be moved into `@wdio/jasmine-framework`. The usage of `expectAsync` is also subject to future removal.
109+
The types `expect-webdriverio/jasmine` are still offered but are subject to removal or being moved into `@wdio/jasmine-framework`. The usage of `expectAsync` is also subject to future removal.
105110

106111
#### Jasmine `expectAsync`
107-
Since the above types augment the `AsyncMatcher` of `Jasmine` then with this library alone it looks like the below even though it is not runnable since the matchers are not registered
112+
Since the above types augment the `AsyncMatcher` of Jasmine, with this library alone it looks like the following, even though it is not runnable since the matchers are not registered:
108113

109114
```ts
110115
describe('My tests', async () => {
@@ -129,7 +134,7 @@ Expected in `tsconfig.json`:
129134
```
130135

131136
#### `expect` of `expect-webdriverio`
132-
It is preferable to use the `expect` from `expect-webdriverio` to guarantee future compatibility
137+
It is preferable to use the `expect` from `expect-webdriverio` to guarantee future compatibility.
133138

134139
```ts
135140
// Required if we do not force the 'expect-webdriverio' expect globally with `"expect-webdriverio/expect-global"`
@@ -150,14 +155,14 @@ Expected in `tsconfig.json`:
150155
"compilerOptions": {
151156
"types": [
152157
"@types/jasmine",
153-
"expect-webdriverio/expect-global", // Force expect to be the 'expect-webdriverio', to comment and use the import above if it conflict with Jasmine
158+
"expect-webdriverio/expect-global", // Force expect to be the 'expect-webdriverio'; comment out and use the import above if it conflicts with Jasmine
154159
]
155160
}
156161
}
157162
```
158163

159164
#### Asymmetric matcher
160-
Asymmetric matcher has limited support, even though `jasmine.stringContaining` has no error it is potentially not working even with `@wdio/jasmine-framework`, but the below should:
165+
Asymmetric matchers have limited support. Even though `jasmine.stringContaining` has no error, it is potentially not working even with `@wdio/jasmine-framework`, but the below should work:
161166

162167
```ts
163168
describe('My tests', async () => {

0 commit comments

Comments
 (0)