Skip to content

Commit

Permalink
fix(expect): use Array.isArray to check if an array is an Array (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Jun 10, 2024
1 parent f342afb commit a3acf57
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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

0 comments on commit a3acf57

Please sign in to comment.