Skip to content

feat(vtz): add vitest-compat mock APIs to @vertz/test (toward #2731)#2748

Merged
viniciusdacal merged 1 commit into
mainfrom
feat/vtz-test-mock-apis
Apr 17, 2026
Merged

feat(vtz): add vitest-compat mock APIs to @vertz/test (toward #2731)#2748
viniciusdacal merged 1 commit into
mainfrom
feat/vtz-test-mock-apis

Conversation

@viniciusdacal
Copy link
Copy Markdown
Contributor

Summary

Real-world test suites written against vitest often call getMockImplementation(), getMockName(), mockName(), and withImplementation() on mock functions. vtz's runtime exposed mock() / vi.fn() without those methods, so tests migrated from vitest hit TypeError: 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, or undefined if none is set. Does not consider the once-queue (matches vitest).
  • getMockName() — returns the display name set via mockName(). Defaults to ''.
  • mockName(name) — sets the display name for diagnostics. Returns the mock for chaining. Cleared by mockReset(); preserved by mockClear().
  • withImplementation(fn, cb) — temporarily swaps the default implementation with fn, runs cb, then restores the original — awaiting cb if it returns a Promise. Returns cb's result. Restores on both sync and async exceptions. Leaves getMockImplementation() unchanged after return.

Exposed on mock(), vi.fn(), and spyOn().

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 MockFunction interface in @vertz/test/src/index.ts:

mockReturnThis(): MockFunction<TArgs, TReturn>;
getMockImplementation(): ((...args: TArgs) => TReturn) | undefined;
getMockName(): string;
mockName(name: string): MockFunction<TArgs, TReturn>;
withImplementation<R>(fn: (...args: TArgs) => TReturn, cb: () => R): R;

No breaking changes. No deferred changes.

Test plan

  • 10 Rust unit tests in native/vtz/src/test/globals.rs covering all four new methods + mockReset/mockClear interactions + sync/async cb restoration + exception paths.
  • 15 TypeScript tests in packages/test/src/__tests__/mock-api.test.ts exercising the full surface via the public @vertz/test import.
  • 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 typecheck on @vertz/test — clean.

Relates to #2731.

🤖 Generated with Claude Code

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>
@viniciusdacal viniciusdacal merged commit d220831 into main Apr 17, 2026
5 of 6 checks passed
This was referenced Apr 17, 2026
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.

1 participant