bun-test-mocking

Solid

Use for mock functions in Bun tests, spyOn, mock.module, implementations, and test doubles.

Testing & QA 162 stars 25 forks Updated 2 weeks ago MIT

Install

View on GitHub

Quality Score: 88/100

Stars 20%
74
Recency 20%
90
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
80
License 10%
100
Description 5%
100

Skill Content

# Bun Test Mocking Bun provides Jest-compatible mocking with `mock()`, `spyOn()`, and module mocking. ## Mock Functions ```typescript 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"); }); ``` ### jest.fn() Compatibility ```typescript 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(); }); ``` ## Mock Return Values ```typescript 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")); ``` ## Mock Implementations ```typescript 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"); ``` ## Spy on Methods ```typescript import { test, expect, spyOn } from "bun:test"; const o...

Details

Author
secondsky
Repository
secondsky/claude-skills
Created
6 months ago
Last Updated
2 weeks ago
Language
TypeScript
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category