Skip to content

Commit

Permalink
Fix Array matching
Browse files Browse the repository at this point in the history
Fixes matching type Array for arrays created in a new context.
  • Loading branch information
popeeyy committed May 27, 2024
1 parent b7ae0b8 commit b791c90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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 @@ -21,6 +21,7 @@ import {
stringNotContaining,
stringNotMatching,
} from '../asymmetricMatchers';
import { runInNewContext } from 'node:vm';

Check failure on line 24 in packages/expect/src/__tests__/asymmetricMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Lint

`node:vm` import should occur before import of `../`

Check failure on line 24 in packages/expect/src/__tests__/asymmetricMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `·runInNewContext·` with `runInNewContext`

test('Any.asymmetricMatch()', () => {
class Thing {}
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)

Check failure on line 140 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `;`
}

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 b791c90

Please sign in to comment.