diff --git a/CHANGELOG.md b/CHANGELOG.md index b8d8ad5f38b4..8c0004c09728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index d4af18c3994f..2a1748e27741 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -6,6 +6,7 @@ * */ +import {runInNewContext} from 'node:vm'; import jestExpect from '../'; import { any, @@ -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); diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index 7a52e4f8b2e2..92f7a67f539d 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -136,6 +136,10 @@ class Any extends AsymmetricMatcher { return typeof other == 'object'; } + if (this.sample == Array) { + return Array.isArray(other); + } + return other instanceof this.sample; } @@ -164,6 +168,10 @@ class Any extends AsymmetricMatcher { return 'boolean'; } + if (Array.isArray(this.sample)) { + return 'array'; + } + return fnNameFor(this.sample); }