Skip to content

Commit 2e2b17a

Browse files
authored
fix: not to break on expect matcher extension overwrite (#11978)
1 parent a1829e9 commit 2e2b17a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
### Fixes
66

7+
- `[expect]` Make expect extension properties `configurable` ([#11978](https://github.com/facebook/jest/pull/11978))
8+
79
### Chore & Maintenance
810

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

1113
### Performance
1214

1315
## 27.3.0
1416

1517
### Features
1618

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

2022
### Fixes

packages/expect/src/__tests__/extend.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,21 @@ it('prints the Symbol into the error message', () => {
167167
}),
168168
).toThrowErrorMatchingSnapshot();
169169
});
170+
171+
it('allows overriding existing extension', () => {
172+
jestExpect.extend({
173+
toAllowOverridingExistingMatcher(_expected: unknown) {
174+
return {pass: _expected === 'bar'};
175+
},
176+
});
177+
178+
jestExpect('foo').not.toAllowOverridingExistingMatcher();
179+
180+
jestExpect.extend({
181+
toAllowOverridingExistingMatcher(_expected: unknown) {
182+
return {pass: _expected === 'foo'};
183+
},
184+
});
185+
186+
jestExpect('foo').toAllowOverridingExistingMatcher();
187+
});

packages/expect/src/jestMatchersObject.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ export const setMatchers = <State extends MatcherState = MatcherState>(
9999
}
100100

101101
Object.defineProperty(expect, key, {
102+
configurable: true,
103+
enumerable: true,
102104
value: (...sample: [unknown, ...Array<unknown>]) =>
103105
new CustomMatcher(false, ...sample),
106+
writable: true,
104107
});
105108
Object.defineProperty(expect.not, key, {
109+
configurable: true,
110+
enumerable: true,
106111
value: (...sample: [unknown, ...Array<unknown>]) =>
107112
new CustomMatcher(true, ...sample),
113+
writable: true,
108114
});
109115
}
110116
});

0 commit comments

Comments
 (0)