bun-test-mocking
FeaturedUse for mock functions in Bun tests, spyOn, mock.module, implementations, and test doubles.
Testing & QA 221 stars
31 forks Updated today MIT
Install
Quality Score: 91/100
Stars 20%
Recency 20%
Frontmatter 20%
Documentation 15%
Issue Health 10%
License 10%
Description 5%
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
- 10 months ago
- Last Updated
- today
- Language
- TypeScript
- License
- MIT
Integrates with
Similar Skills
Semantically similar based on skill content — not just same category
Testing & QA Listed
testing-bun
Use when writing or modifying tests in a Bun project
3 Updated today
LandonSchropp Testing & QA Featured
bun-jest-migration
Use when migrating from Jest to Bun's test runner, import compatibility, mocks, and config.
221 Updated today
secondsky Testing & QA Featured
bun-test-basics
Use for bun:test syntax, assertions, describe/it, test.skip/only/each, and basic patterns.
221 Updated today
secondsky