Skip to content

fix(jest-mock): add construct signature to MockedClass#16189

Open
eyupcanakman wants to merge 2 commits into
jestjs:mainfrom
eyupcanakman:fix/mockedclass-construct-signature
Open

fix(jest-mock): add construct signature to MockedClass#16189
eyupcanakman wants to merge 2 commits into
jestjs:mainfrom
eyupcanakman:fix/mockedclass-construct-signature

Conversation

@eyupcanakman
Copy link
Copy Markdown

Summary

Fixes #14458.

const M = Foo as Mocked<typeof Foo>; const x = new M(); typed x as Foo, not a mocked instance, so x.method.mock was not visible. MockedClass<T> had a call signature but no new signature, so new fell through to the original constructor via MockedObject<T>.

The fix adds a construct signature returning Mocked<InstanceType<T>>, matching the existing call signature.

Classes with overloaded constructors still resolve to the original type, since MockedObject<T> includes the raw class and its construct signatures take precedence. Single-constructor classes, including the reported case, are fixed.

Test plan

yarn test-types for jest-mock. The existing class type test in Mocked.test.ts drops a now-unnecessary cast and asserts the constructed instance is Mocked<InstanceType<typeof MockSomeClass>>; it fails without the signature and passes with it.

MockedClass had a call signature but no `new` signature, so constructing a mocked class returned the original instance type instead of a mocked one. Add a construct signature returning Mocked<InstanceType<T>>.

Closes jestjs#14458
@netlify
Copy link
Copy Markdown

netlify Bot commented May 16, 2026

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 4b82f14
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a08f23308db090008a4a5d7
😎 Deploy Preview https://deploy-preview-16189--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 16, 2026

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 16, 2026

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: 4b82f14

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]: Constructing a mocked class does not return an object typed as a mock, but rather one typed with the original class' type

1 participant