Skip to content

Commit

Permalink
[expect] Fix .any() checks on primitive wrapper classes (#11976)
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie authored Oct 19, 2021
1 parent 2e2b17a commit 7092dfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Number>');
});
Expand Down
14 changes: 7 additions & 7 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ class Any extends AsymmetricMatcher<any> {
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;
Expand Down

0 comments on commit 7092dfb

Please sign in to comment.