Skip to content

fix(jest-mock): type constructed mocked class instances#16212

Open
KirtiRamchandani wants to merge 1 commit into
jestjs:mainfrom
KirtiRamchandani:fix/jest-mocked-class-constructor-type
Open

fix(jest-mock): type constructed mocked class instances#16212
KirtiRamchandani wants to merge 1 commit into
jestjs:mainfrom
KirtiRamchandani:fix/jest-mocked-class-constructor-type

Conversation

@KirtiRamchandani

Copy link
Copy Markdown

Summary

Fixes #14458.

MockedClass<T> already typed mock calls with constructor parameters, but it did not expose a construct signature returning a mocked instance. As a result, constructing a class returned by ModuleMocker.generateFromMetadata() was typed as the original instance type, so mocked methods like instance.memberB.mock were missing at the type level even though they exist at runtime.

This adds the missing construct signature and covers the regression in the jest-mock TSTyche tests.

Test plan

  • corepack yarn tsc -b packages\expect-utils packages\jest-types packages\jest-util packages\jest-mock
  • corepack yarn tstyche packages\jest-mock\__typetests__\ModuleMocker.test.ts packages\jest-mock\__typetests__\Mocked.test.ts
  • corepack yarn tstyche packages/jest-mock/__typetests__
  • corepack yarn eslint packages\jest-mock\src\index.ts packages\jest-mock\__typetests__\ModuleMocker.test.ts
  • corepack yarn jest packages\jest-mock\src\__tests__\index.test.ts --runInBand
  • git diff --check

@netlify

netlify Bot commented May 24, 2026

Copy link
Copy Markdown

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit b8f301e
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a134b0d6ecf7f00085a82ae
😎 Deploy Preview https://deploy-preview-16212--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.

@github-actions github-actions Bot added the require-changelog If a PR does requires a changelog entry label May 24, 2026
@KirtiRamchandani
KirtiRamchandani force-pushed the fix/jest-mocked-class-constructor-type branch from 31e2a7b to b8f301e Compare May 24, 2026 19:01
@pkg-pr-new

pkg-pr-new Bot commented May 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: b8f301e

@KirtiRamchandani

Copy link
Copy Markdown
Author

The remaining CI failure looks unrelated to this PR's type-only change. The failed macOS with shard 3/3 / Node v24.x job retries yarn test-ci-partial:parallel --max-workers 3 --shard=3/3; each attempt ends with a worker SIGSEGV in unrelated suites (packages/jest-util/src/__tests__/globsToMatcher.test.ts, packages/jest-circus/src/__tests__/utils.test.ts, e2e/__tests__/environmentTeardownError.test.ts, e2e/__tests__/testRetries.test.ts). All other checks, including Require Changelog and Codecov, are green.

I don't see a code change needed in this branch from that log; this looks like a macOS/Node runner crash and should be safe to rerun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

require-changelog If a PR does requires a changelog entry

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