fix(expect): accept class instances in toMatchObject and objectContaining#16196
Merged
Conversation
…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
✅ Deploy Preview for jestjs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
|
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
mrazauskas
reviewed
May 19, 2026
SimenB
approved these changes
May 19, 2026
Member
SimenB
left a comment
There was a problem hiding this comment.
moved the tests and added a note in the copilot instructions so maybe it'll catch it next time 🙂
SimenB
reviewed
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #16195.
toMatchObjectandobjectContainingare typed asRecord<string, unknown>requires an index signature, which class instances don't have. So the issue reporter'sexpect(request.session).toMatchObject(session)(wheresessionis aSessionclass instance fromclass-transformer) fails type-checking withTS2345: 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:so the public types are stricter than the impl. Widening to
object | Array<object>(andobjectfor the asymmetric variant) matches the runtime, which already iteratesObject.keys(expected)and works for class instances.Type tests added under
packages/expect/__typetests__/expect.test.tscover the class-instance case for both matchers.Verified:
FORCE_COLOR=1 yarn jest packages/expect→ 1097 passedyarn test-types→ 84 passed, 1581 assertionsyarn lint,yarn check-changelog,yarn check-copyright-headers→ cleanChangelog entry added under
## main/### Fixes, alphabetized by package.