mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
## Summary - Add `[Symbol.dispose]` to mock function prototype, aliased to `mockRestore` - Enables `using spy = spyOn(obj, "method")` to auto-restore when leaving scope - Works for both `spyOn()` and `mock()` Addresses #6040 — gives users a clean way to scope spy lifetimes instead of manually calling `mockRestore()` or relying on `afterEach`. ### Example ```ts import { spyOn, expect, test } from "bun:test"; test("auto-restores spy", () => { const obj = { method: () => "original" }; { using spy = spyOn(obj, "method").mockReturnValue("mocked"); expect(obj.method()).toBe("mocked"); } // automatically restored expect(obj.method()).toBe("original"); }); ``` ## Test plan - `bun bd test test/js/bun/test/mock-disposable.test.ts` — 3 tests pass - Verified tests fail with `USE_SYSTEM_BUN=1`