Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expect] Fix .any() checks on primitive wrapper classes #11976

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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