npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rageltd/bun-test-utils

v2.0.0

Published

A collection of test utilities for Bun projects

Readme

@rageltd/bun-test-utils

Tests Release npm

A collection of test utilities for Bun projects, designed to work around common issues with bun:test mocking and provide helpful testing patterns.

Installation

bun install @rageltd/bun-test-utils

API Reference

Module Mocking

createModuleMocker(): ModuleMocker

Create a module mocker with proper cleanup for handling bun:test mocking issues.

import { createModuleMocker } from '@rageltd/bun-test-utils';

const mockModules = createModuleMocker();

// Mock a module
await mockModules.mock('@/hooks', () => ({
  useUser: () => ({ id: 1, name: 'Test User' })
}));

// Restore specific module
mockModules.restore('@/hooks');

// Restore all modules
mockModules.restoreAll();

restoreModules(modulesMap: Record<string, unknown>): void

Pattern for manually restoring modules to their original state.

import { restoreModules } from '@rageltd/bun-test-utils';

// Store originals
const originals = {
  hooks: await import('@/hooks'),
  utils: await import('@/utils'),
};

// Restore after tests
restoreModules({
  '@/hooks': originals.hooks,
  '@/utils': originals.utils,
});

clearMockRegistry(): void

Clear the mock registry (useful for test cleanup).

import { clearMockRegistry } from '@rageltd/bun-test-utils';

afterEach(() => {
  clearMockRegistry();
});

Cleanup Utilities

setupTestCleanup(): void

Setup automatic cleanup for tests (call once per test file).

import { setupTestCleanup } from '@rageltd/bun-test-utils';

setupTestCleanup(); // Call at top of test file

withMockCleanup(testSuiteFn: () => void): void

Higher-order function to create test suites with proper mock cleanup.

import { withMockCleanup, createModuleMocker } from '@rageltd/bun-test-utils';

withMockCleanup(() => {
  describe('My Test Suite', () => {
    // Your tests here - cleanup is handled automatically
  });
});

Usage Examples

Basic Testing Pattern

import { 
  createModuleMocker, 
  setupTestCleanup,
  clearMockRegistry
} from '@rageltd/bun-test-utils';

// Setup cleanup once per file
setupTestCleanup();

const mockModules = createModuleMocker();

describe('Component Tests', () => {
  beforeEach(async () => {
    await mockModules.mock('@/hooks', () => ({
      useUser: () => ({ id: 1, name: 'Test User' })
    }));
  });

  afterEach(() => {
    clearMockRegistry();
  });

  afterAll(() => {
    mockModules.restoreAll();
  });

  it('should handle mocked modules', () => {
    // Your test here
  });
});

Using withMockCleanup Pattern

import { withMockCleanup, createModuleMocker } from '@rageltd/bun-test-utils';

withMockCleanup(() => {
  describe('My Test Suite', () => {
    const mockModules = createModuleMocker();

    beforeEach(async () => {
      await mockModules.mock('@/services', () => ({
        apiService: { getData: () => Promise.resolve({ data: 'test' }) }
      }));
    });

    it('should work with automatic cleanup', () => {
      // Test implementation
    });
  });
});

Known Issues

This package addresses the known bug in bun:test where mock.restore() doesn't properly restore modules that were mocked with mock.module().

See: https://github.com/oven-sh/bun/issues/7823

Development

# Install dependencies
bun install

# Build
bun run build

# Test
bun test

# Lint
bun run lint

Contributing

This project uses semantic-release for automated versioning and publishing based on conventional commits.

Conventional Commits

Use the interactive commit tool:

bun run commit

Or format commits manually:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Commit Types for Releases:

  • fix: - Bug fixes (patch release: 1.0.1)
  • feat: - New features (minor release: 1.1.0)
  • BREAKING CHANGE or ! - Breaking changes (major release: 2.0.0)
  • Other types (docs:, style:, refactor:, test:, chore:) don't trigger releases

Workflow

  1. Fork and create a feature branch
  2. Install dependencies: bun install
  3. Make changes and add tests
  4. Commit using conventional format: bun run commit
  5. Push and open a Pull Request

CI will automatically handle testing, building, and releasing when merged to main.

Features

  • 🚀 Built with Bun for performance
  • 📦 Dual package (ESM + CommonJS) support
  • 🔧 Full TypeScript support
  • 🧪 Module mocking utilities
  • 🔄 Automatic cleanup utilities
  • ⚡ Automated CI/CD with semantic versioning