Skip to content

Commit

Permalink
Don't invoke expect.extend (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
keeganwitt authored Oct 11, 2021
1 parent 9966215 commit a2904bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,31 @@ yarn add -D jest-extended

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

Add `jest-extended` to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)
```javascript
// ./testSetup.js

// add all jest-extended matchers
import * as matchers from 'jest-extended';
expect.extend(matchers);

// or just add specific matchers
import { toBeArray, toBeSealed } from 'jest-extended';
expect.extend({ toBeArray, toBeSealed });
```

Add your setup script to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)

```json
"jest": {
"setupFilesAfterEnv": ["./testSetup.js"]
}
```

To automatically extend `expect` with all matchers, you can use

```json
"jest": {
"setupFilesAfterEnv": ["jest-extended"]
"setupFilesAfterEnv": ["jest-extended/all"]
}
```

Expand Down
1 change: 1 addition & 0 deletions all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./dist/all');
13 changes: 13 additions & 0 deletions src/all/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as matchers from '../matchers';

const jestExpect = global.expect;

if (jestExpect !== undefined) {
jestExpect.extend(matchers);
} else {
throw new Error(
"Unable to find Jest's global expect. " +
'Please check you have added jest-extended correctly to your jest configuration. ' +
'See https://github.com/jest-community/jest-extended#setup for help.',
);
}
16 changes: 1 addition & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
import * as matchers from './matchers';

const jestExpect = global.expect;

if (jestExpect !== undefined) {
jestExpect.extend(matchers);
} else {
/* eslint-disable no-console */
console.error(
"Unable to find Jest's global expect." +
'\nPlease check you have added jest-extended correctly to your jest configuration.' +
'\nSee https://github.com/jest-community/jest-extended#setup for help.',
);
/* eslint-enable no-console */
}
export * from './matchers';

0 comments on commit a2904bd

Please sign in to comment.