Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
859eacf789 Fix missing mock.clearAllMocks property
Fixes #21437 by adding the missing clearAllMocks method to the mock object.

The clearAllMocks function was already implemented in jest.zig and available
on the jest object, but was not exposed on the mock object despite being
documented. This commit:

1. Adds clearAllMocks to the mock object type definition in test.d.ts
2. Exposes the existing clearAllMocks function on the mock object in jest.zig
3. Adds regression tests to verify the functionality

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-02 01:11:45 +00:00
3 changed files with 44 additions and 0 deletions

View File

@@ -56,6 +56,10 @@ declare module "bun:test" {
* Restore the previous value of mocks.
*/
restore(): void;
/**
* Clear all mock calls.
*/
clearAllMocks(): void;
};
/**

View File

@@ -462,6 +462,7 @@ pub const Jest = struct {
module.put(globalObject, ZigString.static("mock"), mockFn);
mockFn.put(globalObject, ZigString.static("module"), mockModuleFn);
mockFn.put(globalObject, ZigString.static("restore"), restoreAllMocks);
mockFn.put(globalObject, ZigString.static("clearAllMocks"), clearAllMocks);
const jest = JSValue.createEmptyObject(globalObject, 8);
jest.put(globalObject, ZigString.static("fn"), mockFn);

View File

@@ -0,0 +1,39 @@
import { test, expect, mock } from "bun:test";
test("mock.clearAllMocks should exist and be callable", () => {
// Create a mock function
const mockFn = mock(() => "test");
// Call the mock
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
// Test that clearAllMocks exists and is callable
expect(typeof mock.clearAllMocks).toBe("function");
// Call clearAllMocks
mock.clearAllMocks();
// Verify that the mock was cleared
expect(mockFn).toHaveBeenCalledTimes(0);
});
test("mock.clearAllMocks should work the same as jest.clearAllMocks", () => {
const mockFn = mock(() => "test");
// Call the mock
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
// Use mock.clearAllMocks
mock.clearAllMocks();
expect(mockFn).toHaveBeenCalledTimes(0);
// Call the mock again
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
// Use jest.clearAllMocks to verify they do the same thing
jest.clearAllMocks();
expect(mockFn).toHaveBeenCalledTimes(0);
});