Skip to content

Commit

Permalink
fix: not to break on expect matcher extension overwrite (#11978)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaky committed Oct 19, 2021
1 parent a1829e9 commit 2e2b17a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Expand Up @@ -4,17 +4,19 @@

### Fixes

- `[expect]` Make expect extension properties `configurable` ([#11978](https://github.com/facebook/jest/pull/11978))

### Chore & Maintenance

- `[jest-config, jest-util]` Use `ci-info` instead of `is-ci` to detect CI environment ([11973](https://github.com/facebook/jest/pull/11973))
- `[jest-config, jest-util]` Use `ci-info` instead of `is-ci` to detect CI environment ([#11973](https://github.com/facebook/jest/pull/11973))

### Performance

## 27.3.0

### Features

- `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950))
- `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([#11950](https://github.com/facebook/jest/pull/11950))
- `[jest-resolver]` Support default export (`.`) in `exports` field _if_ `main` is missing ([#11919](https://github.com/facebook/jest/pull/11919))

### Fixes
Expand Down
18 changes: 18 additions & 0 deletions packages/expect/src/__tests__/extend.test.ts
Expand Up @@ -167,3 +167,21 @@ it('prints the Symbol into the error message', () => {
}),
).toThrowErrorMatchingSnapshot();
});

it('allows overriding existing extension', () => {
jestExpect.extend({
toAllowOverridingExistingMatcher(_expected: unknown) {
return {pass: _expected === 'bar'};
},
});

jestExpect('foo').not.toAllowOverridingExistingMatcher();

jestExpect.extend({
toAllowOverridingExistingMatcher(_expected: unknown) {
return {pass: _expected === 'foo'};
},
});

jestExpect('foo').toAllowOverridingExistingMatcher();
});
6 changes: 6 additions & 0 deletions packages/expect/src/jestMatchersObject.ts
Expand Up @@ -99,12 +99,18 @@ export const setMatchers = <State extends MatcherState = MatcherState>(
}

Object.defineProperty(expect, key, {
configurable: true,
enumerable: true,
value: (...sample: [unknown, ...Array<unknown>]) =>
new CustomMatcher(false, ...sample),
writable: true,
});
Object.defineProperty(expect.not, key, {
configurable: true,
enumerable: true,
value: (...sample: [unknown, ...Array<unknown>]) =>
new CustomMatcher(true, ...sample),
writable: true,
});
}
});
Expand Down

0 comments on commit 2e2b17a

Please sign in to comment.