Bun Test Mocking by secondsky/claude-skills
npx skills add https://github.com/secondsky/claude-skills --skill 'Bun Test Mocking'Bun 通过 mock()、spyOn() 和模块模拟提供了与 Jest 兼容的模拟功能。
import { test, expect, mock } from "bun:test";
// 创建模拟函数
const fn = mock(() => "original");
test("mock function", () => {
fn("arg1", "arg2");
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith("arg1", "arg2");
});
import { test, expect, jest } from "bun:test";
const fn = jest.fn(() => "value");
test("jest.fn works", () => {
const result = fn();
expect(result).toBe("value");
expect(fn).toHaveBeenCalled();
});
const fn = mock();
// 单次返回值
fn.mockReturnValueOnce("first");
fn.mockReturnValueOnce("second");
// 永久返回值
fn.mockReturnValue("default");
// Promise 返回值
fn.mockResolvedValue("resolved");
fn.mockResolvedValueOnce("once");
fn.mockRejectedValue(new Error("fail"));
fn.mockRejectedValueOnce(new Error("once"));
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
const fn = mock();
// 设置实现
fn.mockImplementation((x) => x * 2);
// 一次性实现
fn.mockImplementationOnce((x) => x * 10);
// 链式实现
fn
.mockImplementationOnce(() => "first")
.mockImplementationOnce(() => "second")
.mockImplementation(() => "default");
import { test, expect, spyOn } from "bun:test";
const obj = {
method: () => "original",
};
test("spy on method", () => {
const spy = spyOn(obj, "method");
obj.method();
expect(spy).toHaveBeenCalled();
expect(obj.method()).toBe("original"); // 仍然有效
// 覆盖实现
spy.mockImplementation(() => "mocked");
expect(obj.method()).toBe("mocked");
// 恢复
spy.mockRestore();
expect(obj.method()).toBe("original");
});
import { test, expect, mock } from "bun:test";
// 模拟整个模块
mock.module("./utils", () => ({
add: mock(() => 999),
subtract: mock(() => 0),
}));
// 现在导入使用模拟版本
import { add } from "./utils";
test("mocked module", () => {
expect(add(1, 2)).toBe(999);
});
mock.module("axios", () => ({
default: {
get: mock(() => Promise.resolve({ data: "mocked" })),
post: mock(() => Promise.resolve({ data: "created" })),
},
}));
mock.module("./config", () => {
return {
API_URL: "http://test.local",
DEBUG: true,
};
});
const fn = mock();
fn("a", "b");
fn("c", "d");
// 调用次数
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(2);
// 调用参数
expect(fn).toHaveBeenCalledWith("a", "b");
expect(fn).toHaveBeenLastCalledWith("c", "d");
expect(fn).toHaveBeenNthCalledWith(1, "a", "b");
// 返回值
fn.mockReturnValue("result");
fn();
expect(fn).toHaveReturned();
expect(fn).toHaveReturnedWith("result");
expect(fn).toHaveReturnedTimes(1);
const fn = mock(() => "value");
fn("arg1");
fn("arg2");
// 访问调用信息
fn.mock.calls; // [["arg1"], ["arg2"]]
fn.mock.results; // [{ type: "return", value: "value" }, ...]
fn.mock.lastCall; // ["arg2"]
// 清除历史记录
fn.mockClear(); // 清除调用记录,保留实现
fn.mockReset(); // 清除调用记录和实现
fn.mockRestore(); // 恢复原始函数(用于监视器)
import { test, expect, spyOn } from "bun:test";
test("mock fetch", async () => {
const spy = spyOn(global, "fetch").mockResolvedValue(
new Response(JSON.stringify({ data: "mocked" }))
);
const response = await fetch("/api/data");
const json = await response.json();
expect(json.data).toBe("mocked");
expect(spy).toHaveBeenCalledWith("/api/data");
spy.mockRestore();
});
test("mock console", () => {
const spy = spyOn(console, "log");
console.log("test message");
expect(spy).toHaveBeenCalledWith("test message");
spy.mockRestore();
});
class UserService {
async getUser(id: string) {
// 真实实现
}
}
test("mock class method", () => {
const service = new UserService();
const spy = spyOn(service, "getUser").mockResolvedValue({
id: "1",
name: "Test User",
});
const user = await service.getUser("1");
expect(user.name).toBe("Test User");
expect(spy).toHaveBeenCalledWith("1");
});
| 错误 | 原因 | 修复方法 |
|---|---|---|
Cannot spy on undefined | 属性不存在 | 检查属性名称 |
Not a function | 尝试模拟非函数 | 使用正确的模拟方法 |
Already mocked | 重复模拟 | 先使用 mockRestore |
Module not found | 模块路径错误 | 检查 mock.module 中的路径 |
加载 references/mock-api.md 当需要:
加载 references/module-mocking.md 当需要:
每周安装量
–
代码仓库
GitHub 星标数
93
首次出现
–
安全审计
Bun provides Jest-compatible mocking with mock(), spyOn(), and module mocking.
import { test, expect, mock } from "bun:test";
// Create mock function
const fn = mock(() => "original");
test("mock function", () => {
fn("arg1", "arg2");
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith("arg1", "arg2");
});
import { test, expect, jest } from "bun:test";
const fn = jest.fn(() => "value");
test("jest.fn works", () => {
const result = fn();
expect(result).toBe("value");
expect(fn).toHaveBeenCalled();
});
const fn = mock();
// Return value once
fn.mockReturnValueOnce("first");
fn.mockReturnValueOnce("second");
// Permanent return value
fn.mockReturnValue("default");
// Promise returns
fn.mockResolvedValue("resolved");
fn.mockResolvedValueOnce("once");
fn.mockRejectedValue(new Error("fail"));
fn.mockRejectedValueOnce(new Error("once"));
const fn = mock();
// Set implementation
fn.mockImplementation((x) => x * 2);
// One-time implementation
fn.mockImplementationOnce((x) => x * 10);
// Chain implementations
fn
.mockImplementationOnce(() => "first")
.mockImplementationOnce(() => "second")
.mockImplementation(() => "default");
import { test, expect, spyOn } from "bun:test";
const obj = {
method: () => "original",
};
test("spy on method", () => {
const spy = spyOn(obj, "method");
obj.method();
expect(spy).toHaveBeenCalled();
expect(obj.method()).toBe("original"); // Still works
// Override implementation
spy.mockImplementation(() => "mocked");
expect(obj.method()).toBe("mocked");
// Restore
spy.mockRestore();
expect(obj.method()).toBe("original");
});
import { test, expect, mock } from "bun:test";
// Mock entire module
mock.module("./utils", () => ({
add: mock(() => 999),
subtract: mock(() => 0),
}));
// Now imports use mocked version
import { add } from "./utils";
test("mocked module", () => {
expect(add(1, 2)).toBe(999);
});
mock.module("axios", () => ({
default: {
get: mock(() => Promise.resolve({ data: "mocked" })),
post: mock(() => Promise.resolve({ data: "created" })),
},
}));
mock.module("./config", () => {
return {
API_URL: "http://test.local",
DEBUG: true,
};
});
const fn = mock();
fn("a", "b");
fn("c", "d");
// Call count
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(2);
// Call arguments
expect(fn).toHaveBeenCalledWith("a", "b");
expect(fn).toHaveBeenLastCalledWith("c", "d");
expect(fn).toHaveBeenNthCalledWith(1, "a", "b");
// Return values
fn.mockReturnValue("result");
fn();
expect(fn).toHaveReturned();
expect(fn).toHaveReturnedWith("result");
expect(fn).toHaveReturnedTimes(1);
const fn = mock(() => "value");
fn("arg1");
fn("arg2");
// Access call info
fn.mock.calls; // [["arg1"], ["arg2"]]
fn.mock.results; // [{ type: "return", value: "value" }, ...]
fn.mock.lastCall; // ["arg2"]
// Clear history
fn.mockClear(); // Clear calls, keep implementation
fn.mockReset(); // Clear calls + implementation
fn.mockRestore(); // Restore original (for spies)
import { test, expect, spyOn } from "bun:test";
test("mock fetch", async () => {
const spy = spyOn(global, "fetch").mockResolvedValue(
new Response(JSON.stringify({ data: "mocked" }))
);
const response = await fetch("/api/data");
const json = await response.json();
expect(json.data).toBe("mocked");
expect(spy).toHaveBeenCalledWith("/api/data");
spy.mockRestore();
});
test("mock console", () => {
const spy = spyOn(console, "log");
console.log("test message");
expect(spy).toHaveBeenCalledWith("test message");
spy.mockRestore();
});
class UserService {
async getUser(id: string) {
// Real implementation
}
}
test("mock class method", () => {
const service = new UserService();
const spy = spyOn(service, "getUser").mockResolvedValue({
id: "1",
name: "Test User",
});
const user = await service.getUser("1");
expect(user.name).toBe("Test User");
expect(spy).toHaveBeenCalledWith("1");
});
| Error | Cause | Fix |
|---|---|---|
Cannot spy on undefined | Property doesn't exist | Check property name |
Not a function | Trying to mock non-function | Use correct mock approach |
Already mocked | Double mocking | Use mockRestore first |
Module not found | Wrong module path | Check path in mock.module |
Load references/mock-api.md when:
Load references/module-mocking.md when:
Weekly Installs
–
Repository
GitHub Stars
93
First Seen
–
Security Audits
Node.js 环境配置指南:多环境管理、类型安全与最佳实践
10,500 周安装