fix(docs): correct mock documentation examples (#25384)

## Summary

- Fix `mock.mock.args` to `mock.mock.calls` in mock-functions.mdx (the
`.args` property doesn't exist)
- Fix mock.restore example to use module methods instead of spy
functions (calling spy functions after restore returns `undefined`)
- Add missing `vi` import in Vitest compatibility example

## Test plan

- [x] Verified each code block works by running tests against the debug
build

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
robobun
2025-12-06 22:17:51 -08:00
committed by GitHub
parent 8773f7ab65
commit b80cb629c6
2 changed files with 13 additions and 13 deletions

View File

@@ -60,7 +60,7 @@ test("random", async () => {
expect(random).toHaveBeenCalled(); expect(random).toHaveBeenCalled();
expect(random).toHaveBeenCalledTimes(3); expect(random).toHaveBeenCalledTimes(3);
expect(random.mock.args).toEqual([[1], [2], [3]]); expect(random.mock.calls).toEqual([[1], [2], [3]]);
expect(random.mock.results[0]).toEqual({ type: "return", value: a }); expect(random.mock.results[0]).toEqual({ type: "return", value: a });
}); });
``` ```

View File

@@ -428,26 +428,26 @@ test("foo, bar, baz", () => {
const barSpy = spyOn(barModule, "bar"); const barSpy = spyOn(barModule, "bar");
const bazSpy = spyOn(bazModule, "baz"); const bazSpy = spyOn(bazModule, "baz");
// Original values // Original implementations still work
expect(fooSpy).toBe("foo"); expect(fooModule.foo()).toBe("foo");
expect(barSpy).toBe("bar"); expect(barModule.bar()).toBe("bar");
expect(bazSpy).toBe("baz"); expect(bazModule.baz()).toBe("baz");
// Mock implementations // Mock implementations
fooSpy.mockImplementation(() => 42); fooSpy.mockImplementation(() => 42);
barSpy.mockImplementation(() => 43); barSpy.mockImplementation(() => 43);
bazSpy.mockImplementation(() => 44); bazSpy.mockImplementation(() => 44);
expect(fooSpy()).toBe(42); expect(fooModule.foo()).toBe(42);
expect(barSpy()).toBe(43); expect(barModule.bar()).toBe(43);
expect(bazSpy()).toBe(44); expect(bazModule.baz()).toBe(44);
// Restore all // Restore all
mock.restore(); mock.restore();
expect(fooSpy()).toBe("foo"); expect(fooModule.foo()).toBe("foo");
expect(barSpy()).toBe("bar"); expect(barModule.bar()).toBe("bar");
expect(bazSpy()).toBe("baz"); expect(bazModule.baz()).toBe("baz");
}); });
``` ```
@@ -455,10 +455,10 @@ Using `mock.restore()` can reduce the amount of code in your tests by adding it
## Vitest Compatibility ## Vitest Compatibility
For added compatibility with tests written for Vitest, Bun provides the `vi` global object as an alias for parts of the Jest mocking API: For added compatibility with tests written for Vitest, Bun provides the `vi` object as an alias for parts of the Jest mocking API:
```ts title="test.ts" icon="/icons/typescript.svg" ```ts title="test.ts" icon="/icons/typescript.svg"
import { test, expect } from "bun:test"; import { test, expect, vi } from "bun:test";
// Using the 'vi' alias similar to Vitest // Using the 'vi' alias similar to Vitest
test("vitest compatibility", () => { test("vitest compatibility", () => {