Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(expect): accept class instances in toMatchObject and objectContaining#16196

Merged
SimenB merged 5 commits into
jestjs:mainfrom
LeSingh1:fix-tomatchobject-class-instances
May 19, 2026
Merged

fix(expect): accept class instances in toMatchObject and objectContaining#16196
SimenB merged 5 commits into
jestjs:mainfrom
LeSingh1:fix-tomatchobject-class-instances

Conversation

@LeSingh1
Copy link
Copy Markdown
Contributor

Refs #16195.

toMatchObject and objectContaining are typed as

toMatchObject(expected: Record<string, unknown> | Array<Record<string, unknown>>): R;
objectContaining(sample: Record<string, unknown>): AsymmetricMatcher;

Record<string, unknown> requires an index signature, which class instances don't have. So the issue reporter's expect(request.session).toMatchObject(session) (where session is a Session class instance from class-transformer) fails type-checking with TS2345: Argument of type 'Session' is not assignable to parameter of type 'Record<string, unknown>'. Index signature for type 'string' is missing in type 'Session'.

The matcher impl already takes object:

// packages/expect/src/matchers.ts:894
toMatchObject(received: object, expected: object) { ... }

so the public types are stricter than the impl. Widening to object | Array<object> (and object for the asymmetric variant) matches the runtime, which already iterates Object.keys(expected) and works for class instances.

Type tests added under packages/expect/__typetests__/expect.test.ts cover the class-instance case for both matchers.

Verified:

  • FORCE_COLOR=1 yarn jest packages/expect → 1097 passed
  • yarn test-types → 84 passed, 1581 assertions
  • yarn lint, yarn check-changelog, yarn check-copyright-headers → clean

Changelog entry added under ## main / ### Fixes, alphabetized by package.

…ning

The parameter type for both was Record<string, unknown>, which rejects
class instances because they lack an index signature. The runtime
implementation already accepts any object, so widening the type to
`object | Array<object>` (and `object` for the asymmetric matcher)
matches reality.

Adds tstyche cases under packages/expect/__typetests__/expect.test.ts
covering Session-style class instances.

Refs jestjs#16195
@netlify
Copy link
Copy Markdown

netlify Bot commented May 19, 2026

Deploy Preview for jestjs ready!

Name Link
🔨 Latest commit a1467ab
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a0c2b573d925c000890c537
😎 Deploy Preview https://deploy-preview-16196--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 19, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: LeSingh1 / name: LeSingh1 (fa60b4d)

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 19, 2026

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@16196

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@16196

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@16196

create-jest

npm i https://pkg.pr.new/create-jest@16196

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@16196

expect

npm i https://pkg.pr.new/expect@16196

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@16196

jest

npm i https://pkg.pr.new/jest@16196

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@16196

jest-circus

npm i https://pkg.pr.new/jest-circus@16196

jest-cli

npm i https://pkg.pr.new/jest-cli@16196

jest-config

npm i https://pkg.pr.new/jest-config@16196

@jest/console

npm i https://pkg.pr.new/@jest/console@16196

@jest/core

npm i https://pkg.pr.new/@jest/core@16196

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@16196

jest-diff

npm i https://pkg.pr.new/jest-diff@16196

jest-docblock

npm i https://pkg.pr.new/jest-docblock@16196

jest-each

npm i https://pkg.pr.new/jest-each@16196

@jest/environment

npm i https://pkg.pr.new/@jest/environment@16196

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@16196

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@16196

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@16196

@jest/expect

npm i https://pkg.pr.new/@jest/expect@16196

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@16196

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@16196

@jest/globals

npm i https://pkg.pr.new/@jest/globals@16196

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@16196

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@16196

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@16196

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@16196

jest-message-util

npm i https://pkg.pr.new/jest-message-util@16196

jest-mock

npm i https://pkg.pr.new/jest-mock@16196

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@16196

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@16196

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@16196

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@16196

jest-resolve

npm i https://pkg.pr.new/jest-resolve@16196

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@16196

jest-runner

npm i https://pkg.pr.new/jest-runner@16196

jest-runtime

npm i https://pkg.pr.new/jest-runtime@16196

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@16196

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@16196

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@16196

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@16196

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@16196

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@16196

@jest/transform

npm i https://pkg.pr.new/@jest/transform@16196

@jest/types

npm i https://pkg.pr.new/@jest/types@16196

jest-util

npm i https://pkg.pr.new/jest-util@16196

jest-validate

npm i https://pkg.pr.new/jest-validate@16196

jest-watcher

npm i https://pkg.pr.new/jest-watcher@16196

jest-worker

npm i https://pkg.pr.new/jest-worker@16196

pretty-format

npm i https://pkg.pr.new/pretty-format@16196

commit: 975cf19

Comment thread packages/expect/__typetests__/expect.test.ts Outdated
Copy link
Copy Markdown
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Copy link
Copy Markdown
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the tests and added a note in the copilot instructions so maybe it'll catch it next time 🙂

Comment thread CHANGELOG.md Outdated
@SimenB SimenB linked an issue May 19, 2026 that may be closed by this pull request
@SimenB SimenB merged commit d43a0ae into jestjs:main May 19, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: toMatchObject does not accept some common types

3 participants