diff --git a/CHANGELOG.md b/CHANGELOG.md index a736ef807758..a82285e1d682 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ ### Fixes -- `[expect]` Make expect extension properties `configurable` ([#11978](https://github.com/facebook/jest/pull/11978)) +- `[expect]` Make `expect` extension properties `configurable` ([#11978](https://github.com/facebook/jest/pull/11978)) +- `[expect]` Fix `.any()` checks on primitive wrapper classes ([#11976](https://github.com/facebook/jest/pull/11976)) ### Chore & Maintenance diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index ec48bc73a624..9c9e7b996509 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -40,6 +40,24 @@ test('Any.asymmetricMatch()', () => { }); }); +test('Any.asymmetricMatch() on primitive wrapper classes', () => { + [ + // eslint-disable-next-line no-new-wrappers + any(String).asymmetricMatch(new String('jest')), + // eslint-disable-next-line no-new-wrappers + any(Number).asymmetricMatch(new Number(1)), + // eslint-disable-next-line no-new-func + any(Function).asymmetricMatch(new Function('() => {}')), + // eslint-disable-next-line no-new-wrappers + any(Boolean).asymmetricMatch(new Boolean(true)), + /* global BigInt */ + any(BigInt).asymmetricMatch(Object(1n)), + any(Symbol).asymmetricMatch(Object(Symbol())), + ].forEach(test => { + jestExpect(test).toBe(true); + }); +}); + test('Any.toAsymmetricMatcher()', () => { jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any'); }); diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index c8d2dae00625..b52c62951d61 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -69,21 +69,21 @@ class Any extends AsymmetricMatcher { return typeof other == 'function' || other instanceof Function; } - if (this.sample == Object) { - return typeof other == 'object'; - } - if (this.sample == Boolean) { - return typeof other == 'boolean'; + return typeof other == 'boolean' || other instanceof Boolean; } /* global BigInt */ if (this.sample == BigInt) { - return typeof other == 'bigint'; + return typeof other == 'bigint' || other instanceof BigInt; } if (this.sample == Symbol) { - return typeof other == 'symbol'; + return typeof other == 'symbol' || other instanceof Symbol; + } + + if (this.sample == Object) { + return typeof other == 'object'; } return other instanceof this.sample;