Test helpers for common testing patterns in Stratix applications.

Provides utilities to simplify test setup, entity creation, and event testing.

describe('ProductService', () => {
it('should create product and emit event', async () => {
const { bus, events } = TestHelpers.createEventBusCapture();
const handler = new CreateProductHandler(mockRepo, bus);

await handler.handle(new CreateProductCommand('Product', 100));

const event = await TestHelpers.waitForEvent(bus, ProductCreatedEvent);
expect(event).toBeDefined();
});
});

Constructors

Methods

  • Creates an entity with minimal boilerplate for testing.

    Type Parameters

    • TEntity extends Entity<string>
    • TProps

    Parameters

    • EntityClass: new (
          id: EntityId<string>,
          props: TProps,
          createdAt: Date,
          updatedAt: Date,
      ) => TEntity

      The entity class to instantiate

    • id: string | EntityId<string>

      Entity ID (string or EntityId)

    • props: TProps

      Entity properties

    • Optionaltimestamps: { createdAt?: Date; updatedAt?: Date }

      Optional timestamps (defaults to now)

    Returns TEntity

    The created entity

    const product = TestHelpers.createEntity(
    Product,
    'product-123',
    { name: 'Test Product', price: 100 }
    );
  • Creates an in-memory command bus for testing.

    Returns CommandBus

    A new InMemoryCommandBus instance

    const commandBus = TestHelpers.createCommandBus();
    commandBus.register(CreateProductCommand, new CreateProductHandler(repo));
    await commandBus.execute(new CreateProductCommand('Product', 100));
  • Creates an in-memory query bus for testing.

    Returns QueryBus

    A new InMemoryQueryBus instance

    const queryBus = TestHelpers.createQueryBus();
    queryBus.register(GetProductQuery, new GetProductHandler(repo));
    const product = await queryBus.execute(new GetProductQuery('product-id'));
  • Creates an event bus that captures all published events.

    Useful for asserting that specific events were published during a test.

    Returns { bus: EventBus; events: Event[] }

    Object with the event bus and array of captured events

    const { bus, events } = TestHelpers.createEventBusCapture();

    await handler.handle(command);

    expect(events).toHaveLength(1);
    expect(events[0]).toBeInstanceOf(ProductCreatedEvent);
  • Waits for a specific event type to be published on an event bus.

    Useful for async event testing where you need to wait for an event before making assertions.

    Type Parameters

    • TEvent extends Event

    Parameters

    • bus: EventBus

      The event bus to monitor

    • EventType: new (...args: any[]) => TEvent

      The event class to wait for

    • timeoutMs: number = 5000

      Maximum time to wait in milliseconds (default: 5000)

    Returns Promise<null | TEvent>

    Promise that resolves with the event or null if timeout

    const bus = new InMemoryEventBus();

    // Trigger async operation that will publish event
    handler.handle(command);

    const event = await TestHelpers.waitForEvent(bus, ProductCreatedEvent);
    expect(event.productId).toBe('product-123');
  • Creates a spy/mock event bus that tracks all published events.

    Different from createEventBusCapture in that it doesn't actually publish events to handlers, just captures them for inspection.

    Returns { bus: EventBus; events: Event[] }

    Object with the mock bus and captured events

    const { bus, events } = TestHelpers.createEventBusSpy();

    await service.createProduct(data); // This publishes events

    expect(events).toContainEqual(expect.objectContaining({
    type: 'ProductCreated'
    }));