secondsky avatar

Bun Test Mocking

Covers Bun's Jest-compatible mocking with mock(), spyOn(), and mock.module() for test doubles. You get standard stuff like mockReturnValue, mockImplementation, and call assertions, plus module-level mocking that works on both local files and node_modules. The spy functionality is clean for checking calls without breaking real behavior, and the mock properties (calls, results, lastCall) give you good introspection. Honestly the most useful part is mock.module() since it handles the hoisting complexity for you. If you're writing Bun tests and need to stub dependencies or track function calls, this covers the patterns without making you dig through docs.

skills.sh installs
335
GitHub stars
248
last updated
Aug 21, 2026

Install

$ npx -y skills add secondsky/claude-skills --skill bun-test-mocking --agent claude-code

Installs into .claude/skills of the current project.

Files

Bun Test Mocking

Bun provides Jest-compatible mocking with mock(), spyOn(), and module mocking.

Mock Functions

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

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

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

const fn = mock();

// Set implementation
fn.mockImplementation((x) => x * 2);

Sponsors

Details

Repository
github.com/secondsky/claude-skills
Category
Testing & QA
First seen
Jun 3, 2026

More from secondsky/claude-skills

All 146
1secondsky avatarbun-jest-migrationsecondsky/claude-skillsUse when migrating from Jest to Bun's test runner, import compatibility, mocks, and config.326skills.sh installs
2davila7 avatartdd-workflowdavila7/claude-code-templatesTest-Driven Development workflow principles. RED-GREEN-REFACTOR cycle.290skills.sh installs
3dhruvanbhalara avatarflutter-testingdhruvanbhalara/skillsDefine testing strategy, test pyramid, and pattern-based conventions (Golden Variants, State Matrix, Interaction Contracts). Use when establishing test architec283skills.sh installs
4dubzzz avatarjavascript-testing-expertdubzzz/fast-checkExpert-level JavaScript testing skill focused on writing high-quality tests that find bugs, serve as documentation, and prevent regressions. Advocates for prope269skills.sh installs
5patricio0312rev avatarintegration-test-builderpatricio0312rev/skillsCreates integration tests for API endpoints with database flows, including test harness setup, fixtures, setup/teardown, database seeding, and CI-friendly strat263skills.sh installs
6patricio0312rev avatarcoverage-strategistpatricio0312rev/skillsDefines ROI-based coverage targets with critical path identification, layer-specific targets, and explicit "don't test this" guidelines. Use for "test coverage"259skills.sh installs