Working with Bun

This guide covers Bun-specific considerations and best practices when using @rageltd/bun-test-utils.

Bun Testing Framework

Bun's built-in testing framework is fast and lightweight, but has some unique characteristics that this library helps address.

Known Issues

  • Module Mocking - mock.restore() doesn't properly restore modules
  • Mock Pollution - Mocks can leak between test files
  • Async Timing - Different timing behavior compared to Node.js

Solutions Provided

  • createModuleMocker() - Handles proper module restoration
  • setupTestCleanup() - Prevents mock pollution
  • clearMockRegistry() - Clears internal mock state

Performance Considerations

Bun is significantly faster than Node.js for most operations. This library is optimized to take advantage of Bun's performance while maintaining compatibility.

Test Execution Speed

# Bun is typically 2-3x faster than Jest
bun test  # ~500ms for 100 tests
jest      # ~1500ms for 100 tests

Configuration

TypeScript Configuration

Ensure your tsconfig.json is optimized for Bun:

{
  "compilerOptions": {
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "jsx": "react-jsx",
    "types": ["bun-types"]
  }
}