From b791c90377f3941bf5a51364aa93ed341380f146 Mon Sep 17 00:00:00 2001 From: popeeyy <29686338+popeeyy@users.noreply.github.com> Date: Mon, 27 May 2024 14:06:19 -0700 Subject: [PATCH] Fix Array matching Fixes matching type Array for arrays created in a new context. --- packages/expect/src/__tests__/asymmetricMatchers.test.ts | 2 ++ packages/expect/src/asymmetricMatchers.ts | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index d4af18c3994f..d5ec9d66c27d 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -21,6 +21,7 @@ import { stringNotContaining, stringNotMatching, } from '../asymmetricMatchers'; +import { runInNewContext } from 'node:vm'; test('Any.asymmetricMatch()', () => { class Thing {} @@ -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..d619bf00573e 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); }