From 2e2b17a950b21dcd5c3d15ed4a83933c474d9885 Mon Sep 17 00:00:00 2001 From: Bence Laky Date: Tue, 19 Oct 2021 08:03:03 +0200 Subject: [PATCH] fix: not to break on `expect` matcher extension overwrite (#11978) --- CHANGELOG.md | 6 ++++-- packages/expect/src/__tests__/extend.test.ts | 18 ++++++++++++++++++ packages/expect/src/jestMatchersObject.ts | 6 ++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08c99d3dc8fe..a736ef807758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ ### 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 @@ -14,7 +16,7 @@ ### 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 diff --git a/packages/expect/src/__tests__/extend.test.ts b/packages/expect/src/__tests__/extend.test.ts index 8fdfd1d856c1..1ddd913ed07a 100644 --- a/packages/expect/src/__tests__/extend.test.ts +++ b/packages/expect/src/__tests__/extend.test.ts @@ -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(); +}); diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index 3b15af89015e..cbfe6344a6db 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -99,12 +99,18 @@ export const setMatchers = ( } Object.defineProperty(expect, key, { + configurable: true, + enumerable: true, value: (...sample: [unknown, ...Array]) => new CustomMatcher(false, ...sample), + writable: true, }); Object.defineProperty(expect.not, key, { + configurable: true, + enumerable: true, value: (...sample: [unknown, ...Array]) => new CustomMatcher(true, ...sample), + writable: true, }); } });