Skip to content

Commit a2904bd

Browse files
authored
Don't invoke expect.extend (#333)
1 parent 9966215 commit a2904bd

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,31 @@ yarn add -D jest-extended
135135

136136
Note that `jest-extended` only supports Jest version 24 and newer.
137137

138-
Add `jest-extended` to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)
138+
```javascript
139+
// ./testSetup.js
140+
141+
// add all jest-extended matchers
142+
import * as matchers from 'jest-extended';
143+
expect.extend(matchers);
144+
145+
// or just add specific matchers
146+
import { toBeArray, toBeSealed } from 'jest-extended';
147+
expect.extend({ toBeArray, toBeSealed });
148+
```
149+
150+
Add your setup script to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)
151+
152+
```json
153+
"jest": {
154+
"setupFilesAfterEnv": ["./testSetup.js"]
155+
}
156+
```
157+
158+
To automatically extend `expect` with all matchers, you can use
139159

140160
```json
141161
"jest": {
142-
"setupFilesAfterEnv": ["jest-extended"]
162+
"setupFilesAfterEnv": ["jest-extended/all"]
143163
}
144164
```
145165

all.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./dist/all');

src/all/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as matchers from '../matchers';
2+
3+
const jestExpect = global.expect;
4+
5+
if (jestExpect !== undefined) {
6+
jestExpect.extend(matchers);
7+
} else {
8+
throw new Error(
9+
"Unable to find Jest's global expect. " +
10+
'Please check you have added jest-extended correctly to your jest configuration. ' +
11+
'See https://github.com/jest-community/jest-extended#setup for help.',
12+
);
13+
}

src/index.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
import * as matchers from './matchers';
2-
3-
const jestExpect = global.expect;
4-
5-
if (jestExpect !== undefined) {
6-
jestExpect.extend(matchers);
7-
} else {
8-
/* eslint-disable no-console */
9-
console.error(
10-
"Unable to find Jest's global expect." +
11-
'\nPlease check you have added jest-extended correctly to your jest configuration.' +
12-
'\nSee https://github.com/jest-community/jest-extended#setup for help.',
13-
);
14-
/* eslint-enable no-console */
15-
}
1+
export * from './matchers';

0 commit comments

Comments
 (0)