feat(vtz): add vitest-compat mock APIs to @vertz/test (toward #2731)#2748
Merged
Conversation
Adds getMockImplementation, getMockName, mockName, and withImplementation to every mock created by mock()/vi.fn()/spyOn(). Real-world test suites migrated from vitest rely on these — not having them surfaced in #2731 as `mockCreate.getMockImplementation is not a function`. - getMockImplementation returns the current default impl, or undefined. Intentionally ignores the once-queue (matches vitest semantics). - mockName/getMockName provide a settable display name; default "". mockReset clears it, mockClear preserves it. - withImplementation temporarily swaps the default impl, runs cb, restores on both sync/async success and exception paths. Returns cb's result (awaited if Promise). Does not disturb getMockImplementation after return. Also adds type declarations in @vertz/test's MockFunction, 10 new Rust unit tests in native/vtz/src/test/globals.rs, and 15 TS tests in packages/test/src/__tests__/mock-api.test.ts. Not implemented (intentionally): mockThrow/mockThrowOnce and mock.settledResults/instances/contexts/invocationCallOrder — the runtime doesn't track those state streams today. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 17, 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.
Summary
Real-world test suites written against vitest often call
getMockImplementation(),getMockName(),mockName(), andwithImplementation()on mock functions. vtz's runtime exposedmock()/vi.fn()without those methods, so tests migrated from vitest hitTypeError: x.getMockImplementation is not a function(surfaced in #2731).This PR fills that part of the gap. It is not about transitive-mock propagation — another agent is handling that.
New APIs on every mock
getMockImplementation()— returns the current default implementation, orundefinedif none is set. Does not consider the once-queue (matches vitest).getMockName()— returns the display name set viamockName(). Defaults to''.mockName(name)— sets the display name for diagnostics. Returns the mock for chaining. Cleared bymockReset(); preserved bymockClear().withImplementation(fn, cb)— temporarily swaps the default implementation withfn, runscb, then restores the original — awaitingcbif it returns a Promise. Returnscb's result. Restores on both sync and async exceptions. LeavesgetMockImplementation()unchanged after return.Exposed on
mock(),vi.fn(), andspyOn().Not implemented (intentional)
mockThrow/mockThrowOnce(v4.1.0+ vitest) — adds surface without strong use-case yet. Easy to add later.mock.settledResults,mock.instances,mock.contexts,mock.invocationCallOrder— separate state streams the runtime doesn't currently track. Follow-up if demand materializes. Documented in the changeset.Public API changes
Additive to
MockFunctioninterface in@vertz/test/src/index.ts:No breaking changes. No deferred changes.
Test plan
native/vtz/src/test/globals.rscovering all four new methods + mockReset/mockClear interactions + sync/async cb restoration + exception paths.packages/test/src/__tests__/mock-api.test.tsexercising the full surface via the public@vertz/testimport.cargo test -p vtz --lib— 3453 passed.cargo clippy -p vtz --all-targets -- -D warnings— clean.cargo fmt --all -- --check— clean.vtz test packages/test/src/__tests__/mock-api.test.ts— 15/15 pass.vtz run typecheckon @vertz/test — clean.Relates to #2731.
🤖 Generated with Claude Code