Skip to content

fix(testing): distribute DeepMockedInternal over union members - #1292

Open
abu-tahir-0 wants to merge 1 commit into
golevelup:masterfrom
abu-tahir-0:fix/deep-mocked-union-memory
Open

abu-tahir-0 wants to merge 1 commit into
golevelup:masterfrom
abu-tahir-0:fix/deep-mocked-union-memory

Conversation

@abu-tahir-0

Copy link
Copy Markdown

Fixes #1280

Problem

When DeepMocked<T> or createMock<T>() is used with a type T that is a union (especially large unions such as multi-variant domain objects or AST types), tsc type checking consumes massive amounts of heap memory (up to 5+ GB) and time when an array literal or collection holds two or more mock instances. In standard environments without --max-old-space-size, tsc fails with FATAL ERROR: JavaScript heap out of memory.

Root Cause

In packages/testing/ts-jest/src/mocks.ts and packages/testing/ts-vitest/src/type-helper.ts, DeepMockedInternal<T, D> was defined as:

type DeepMockedInternal<T, D> = { [K in keyof T]: ... } & T;

While homomorphic mapped types distribute over union members, the trailing & T was outside the distribution. For a union T = A | B, ({ [K in keyof A]: ... } | { [K in keyof B]: ... }) & (A | B) produces cross-member intersections (mappedA & B, mappedB & A, etc.). For unions with tens of members, this results in hundreds of contradictory cross-terms that explode during pairwise structural comparison when elements are combined in array literals.

Solution

Wrap DeepMockedInternal with distributive conditional type syntax:

type DeepMockedInternal<T, D> = T extends unknown
  ? (... & T)
  : never;

This forces TypeScript to distribute over each union member before evaluating the mapped type and intersecting with T. Each union member is deeply mocked and intersected only with itself, avoiding combinatorial cross-member type generation while preserving full typing on discriminated union members.

Applied to both @golevelup/ts-jest and @golevelup/ts-vitest.

Testing

  • Added unit and type-level test cases in packages/testing/ts-jest/src/mocks.spec.ts and packages/testing/ts-vitest/src/mocks.spec.ts asserting that union types can be mocked, their member properties are intact, and array literals of mocks typecheck and execute cleanly.
  • Verified test suite passes:
    • pnpm jest packages/testing/ts-jest/src/mocks.spec.ts (35/35 passed)
    • pnpm vitest run packages/testing/ts-vitest (21/21 passed)
  • Passed linter checks (pnpm oxlint).

Impact

Significantly reduces TypeScript compiler memory consumption and check time when mocking unions or using collections of mocks, eliminating heap OOM errors for large union structures without breaking existing mock behavior.

Closes golevelup#1280. Distribute over union members before applying
mapped type and & T intersection to avoid combinatorial
cross-member intersection explosion on union types.

This branch has not been deployed

No deployments
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.

[ts-jest] DeepMocked<Union> in an array literal makes tsc use ~5 GB (2.5x regression in 2.0.0/3.0.0)

1 participant