Quick Start

Get up and running with @rageltd/bun-test-utils in just a few minutes. This guide will walk you through your first test using the library's core utilities.

Your First Test

Let's create a simple test that demonstrates the key features of the library.

1. Create a Test File

Create a new test file in your project:

// test/quick-start.test.ts
// test/quick-start.test.ts
import { describe, it, expect, beforeEach, afterAll } from 'bun:test';
import {
  createModuleMocker,
  setupTestCleanup,
  clearMockRegistry
} from '@rageltd/bun-test-utils';

// Setup automatic cleanup for all tests in this file
setupTestCleanup();

const mockModules = createModuleMocker();

describe('Quick Start Example', () => {
  beforeEach(async () => {
    // Mock a module with custom services
    await mockModules.mock('@/services/userService', () => ({
      userService: {
        getUser: () => Promise.resolve({
          id: 1,
          name: 'Test User',
          email: 'test@example.com',
          isActive: true
        })
      }
    }));
  });

  afterAll(() => {
    // Clean up all module mocks
    mockModules.restoreAll();
  });

  it('should handle module mocking', async () => {
    // Module is already mocked in beforeEach
    // Test would verify mocked behavior here
    expect(mockModules).toBeDefined();
    expect(typeof mockModules.restoreAll).toBe('function');
  });

  it('should handle cleanup properly', () => {
    // Test cleanup functionality
    expect(() => clearMockRegistry()).not.toThrow();
    expect(() => mockModules.restoreAll()).not.toThrow();
  });
});

2. Run Your Test

bun test test/quick-start.test.ts

You should see output indicating your tests passed!

Next Steps

Now that you've got the basics down, explore more advanced features: