@rageltd/bun-test-utils
Version: Latest | License: The Unlicense | Runtime: Bun v1.0+
A focused collection of test utilities designed specifically for Bun projects, addressing common module mocking issues and providing automated cleanup patterns.
Overview
This library addresses the unique testing needs of Bun applications by providing:
- Module Mocking Utilities - Work around known issues in
bun:testmodule mocking - Cleanup Management - Automatic test isolation and cleanup
- Test Setup Helpers - Streamlined test configuration patterns
Why This Library?
Bun's testing framework is powerful but has some rough edges, particularly around module mocking and cleanup. This library provides battle-tested utilities that:
- Handle Bun-specific Issues - Works around known bugs in
bun:test - Reduce Boilerplate - Common testing patterns made simple
- Ensure Test Isolation - Proper cleanup and mock restoration
- Type Safety - Full TypeScript support with proper typing
- Performance Focused - Built with Bun's performance in mind
Key Features
🚀 Module Mocking
Properly handle module mocking with automatic cleanup and restoration, working around bun:test limitations.
🧹 Cleanup Management
Automatic cleanup utilities to ensure proper test isolation and mock restoration.
Quick Example
import {
createModuleMocker,
setupTestCleanup,
clearMockRegistry
} from '@rageltd/bun-test-utils';
// Setup automatic cleanup
setupTestCleanup();
const mockModules = createModuleMocker();
describe('User Component', () => {
beforeEach(async () => {
await mockModules.mock('@/services', () => ({
userService: {
getUser: () => Promise.resolve({ id: 1, name: 'Test User' })
}
}));
});
afterEach(() => {
clearMockRegistry();
});
afterAll(() => {
mockModules.restoreAll();
});
it('should handle user service calls', () => {
// Your test here - mocks are properly isolated
});
});
Installation
bun install @rageltd/bun-test-utils
Browser Compatibility
This library is designed for:
- Bun runtime - Primary target with
bun:test - TypeScript projects - Full type safety and IntelliSense
- Modern JavaScript - ESM and CommonJS support
Next Steps
Made with ❤️ for the Bun testing ecosystem