Repository navigation
fix(testing): distribute DeepMockedInternal over union members - #1292
Open
abu-tahir-0 wants to merge 1 commit into
Open
abu-tahir-0 wants to merge 1 commit into
abu-tahir-0 wants to merge 1 commit into
Conversation
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
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.
Fixes #1280
Problem
When
DeepMocked<T>orcreateMock<T>()is used with a typeTthat is a union (especially large unions such as multi-variant domain objects or AST types),tsctype 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,tscfails withFATAL ERROR: JavaScript heap out of memory.Root Cause
In
packages/testing/ts-jest/src/mocks.tsandpackages/testing/ts-vitest/src/type-helper.ts,DeepMockedInternal<T, D>was defined as:While homomorphic mapped types distribute over union members, the trailing
& Twas outside the distribution. For a unionT = 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
DeepMockedInternalwith distributive conditional type syntax: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-jestand@golevelup/ts-vitest.Testing
packages/testing/ts-jest/src/mocks.spec.tsandpackages/testing/ts-vitest/src/mocks.spec.tsasserting that union types can be mocked, their member properties are intact, and array literals of mocks typecheck and execute cleanly.pnpm jest packages/testing/ts-jest/src/mocks.spec.ts(35/35 passed)pnpm vitest run packages/testing/ts-vitest(21/21 passed)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.