Skip to content

Commit 3777bd1

Browse files
committed
fix(expect): use Array.isArray to check if an array is an Array
1 parent fa24a3b commit 3777bd1

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
4444
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
4545
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
46+
- `[expect]` Use `Array.isArray` to check if an array is an `Array` ([#15101](https://github.com/jestjs/jest/pull/15101))
4647
- `[jest-changed-files]` Print underlying errors when VCS commands fail ([#15052](https://github.com/jestjs/jest/pull/15052))
4748
- `[jest-changed-files]` Abort `sl root` call if output resembles a steam locomotive ([#15053](https://github.com/jestjs/jest/pull/15053))
4849
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))

packages/expect/src/__tests__/asymmetricMatchers.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88

9+
import {runInNewContext} from 'node:vm';
910
import jestExpect from '../';
1011
import {
1112
any,
@@ -51,6 +52,7 @@ test('Any.asymmetricMatch() on primitive wrapper classes', () => {
5152
any(Boolean).asymmetricMatch(new Boolean(true)),
5253
any(BigInt).asymmetricMatch(Object(1n)),
5354
any(Symbol).asymmetricMatch(Object(Symbol())),
55+
any(Array).asymmetricMatch(runInNewContext('[];')),
5456
/* eslint-enable */
5557
]) {
5658
jestExpect(test).toBe(true);

packages/expect/src/asymmetricMatchers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class Any extends AsymmetricMatcher<any> {
136136
return typeof other == 'object';
137137
}
138138

139+
if (this.sample == Array) {
140+
return Array.isArray(other);
141+
}
142+
139143
return other instanceof this.sample;
140144
}
141145

@@ -164,6 +168,10 @@ class Any extends AsymmetricMatcher<any> {
164168
return 'boolean';
165169
}
166170

171+
if (Array.isArray(this.sample)) {
172+
return 'array';
173+
}
174+
167175
return fnNameFor(this.sample);
168176
}
169177

0 commit comments

Comments
 (0)