Skip to content
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
fix(expect): use Array.isArray to check if an array is an Array
  • Loading branch information
G-Rath committed May 31, 2024
commit 3777bd1a29c8d9666b42d90874905845fdd645af
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
- `[expect]` Use `Array.isArray` to check if an array is an `Array` ([#15101](https://github.com/jestjs/jest/pull/15101))
- `[jest-changed-files]` Print underlying errors when VCS commands fail ([#15052](https://github.com/jestjs/jest/pull/15052))
- `[jest-changed-files]` Abort `sl root` call if output resembles a steam locomotive ([#15053](https://github.com/jestjs/jest/pull/15053))
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))
Expand Down
2 changes: 2 additions & 0 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {runInNewContext} from 'node:vm';
import jestExpect from '../';
import {
any,
Expand Down Expand Up @@ -51,6 +52,7 @@ test('Any.asymmetricMatch() on primitive wrapper classes', () => {
any(Boolean).asymmetricMatch(new Boolean(true)),
any(BigInt).asymmetricMatch(Object(1n)),
any(Symbol).asymmetricMatch(Object(Symbol())),
any(Array).asymmetricMatch(runInNewContext('[];')),
/* eslint-enable */
]) {
jestExpect(test).toBe(true);
Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class Any extends AsymmetricMatcher<any> {
return typeof other == 'object';
}

if (this.sample == Array) {
return Array.isArray(other);
}

return other instanceof this.sample;
}

Expand Down Expand Up @@ -164,6 +168,10 @@ class Any extends AsymmetricMatcher<any> {
return 'boolean';
}

if (Array.isArray(this.sample)) {
return 'array';
}

return fnNameFor(this.sample);
}

Expand Down