Skip to content

Commit

Permalink
Update toStrictEqual() to be able to check jest.fn().mock.calls etc. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuibu committed Mar 6, 2023
1 parent 5093af4 commit d27e36f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,7 @@
- `[jest-circus]` Update message printed on test timeout ([#13830](https://github.com/facebook/jest/pull/13830))
- `[jest-circus]` Avoid creating the word "testfalse" when `takesDoneCallback` is `false` in the message printed on test timeout AND updated timeouts test ([#13954](https://github.com/facebook/jest/pull/13954))
- `[jest-environment-jsdom]` Stop setting `document` to `null` on teardown ([#13972](https://github.com/facebook/jest/pull/13972))
- `[@jest/expect-utils]` Update `toStrictEqual()` to be able to check `jest.fn().mock.calls` ([#13960](https://github.com/facebook/jest/pull/13960))
- `[@jest/test-result]` Allow `TestResultsProcessor` type to return a Promise ([#13950](https://github.com/facebook/jest/pull/13950))

### Chore & Maintenance
Expand Down
7 changes: 7 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Expand Up @@ -15,6 +15,7 @@ import {
getPath,
iterableEquality,
subsetEquality,
typeEquality,
} from '../utils';

describe('getPath()', () => {
Expand Down Expand Up @@ -546,6 +547,12 @@ describe('iterableEquality', () => {
});
});

describe('typeEquality', () => {
test('returns undefined if given mock.calls and []', () => {
expect(typeEquality(jest.fn().mock.calls, [])).toBeUndefined();
});
});

describe('arrayBufferEquality', () => {
test('returns undefined if given a non instance of ArrayBuffer', () => {
expect(arrayBufferEquality(2, 's')).toBeUndefined();
Expand Down
11 changes: 10 additions & 1 deletion packages/expect-utils/src/utils.ts
Expand Up @@ -370,7 +370,16 @@ export const subsetEquality = (

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const typeEquality = (a: any, b: any): boolean | undefined => {
if (a == null || b == null || a.constructor === b.constructor) {
if (
a == null ||
b == null ||
a.constructor === b.constructor ||
// Since Jest globals are different from Node globals,
// constructors are different even between arrays when comparing properties of mock objects.
// Both of them should be able to compare correctly when they are array-to-array.
// https://github.com/facebook/jest/issues/2549
(Array.isArray(a) && Array.isArray(b))
) {
return undefined;
}

Expand Down

0 comments on commit d27e36f

Please sign in to comment.