Advanced testing techniques

This guide covers advanced testing patterns, utilities, and techniques for comprehensive Aurelia 2 application testing. It builds upon the foundation provided in the basic testing guides and explores sophisticated testing scenarios.

Comprehensive Fixture API Reference

The createFixture function returns a powerful fixture object with extensive testing utilities. Understanding all available methods enables more effective and expressive tests.

Complete Fixture Interface

interface IFixture<T> {
  // Core properties
  appHost: HTMLElement;           // Root element containing your app
  startPromise: Promise<void>;    // Promise that resolves when app starts
  stop: (dispose?: boolean) => Promise<void>; // Cleanup function
  component: T;                   // Root component instance
  container: IContainer;          // DI container for registrations
  platform: IPlatform;           // Platform abstraction
  testContext: TestContext;       // Test context utilities

  // Query methods
  getBy<E extends HTMLElement = HTMLElement>(selector: string): E;
  getAllBy<E extends HTMLElement = HTMLElement>(selector: string): E[];
  queryBy<E extends HTMLElement = HTMLElement>(selector: string): E | null;

  // Assertion methods
  assertText(expectedText: string, options?: ITextAssertOptions): void;
  assertText(selector: string, expectedText: string, options?: ITextAssertOptions): void;
  assertTextContain(expectedText: string): void;
  assertTextContain(selector: string, expectedText: string): void;
  assertHtml(expectedHtml: string, options?: IHtmlAssertOptions): void;
  assertHtml(selector: string, expectedHtml: string, options?: IHtmlAssertOptions): void;
  assertAttr(selector: string, attr: string, expectedValue: string | null): void;
  assertAttrNS(selector: string, namespace: string, attr: string, expectedValue: string | null): void;
  assertClass(selector: string, ...expectedClasses: string[]): void;
  assertClassStrict(selector: string, ...expectedClasses: string[]): void;
  assertStyles(selector: string, expectedStyles: Record<string, string>): void;
  assertValue(selector: string, expectedValue: string): void;
  assertChecked(selector: string, expectedChecked: boolean): void;

  // Event methods
  trigger: IEventTrigger;
  type(selector: string | HTMLElement, text: string): void;
  scrollBy(selector: string | HTMLElement, options: number | ScrollOptions): void;

  // Utility methods
  printHtml(): string;
  createEvent(type: string, init?: EventInit): Event;
  hJsx(tag: string, attrs?: Record<string, any>, ...children: any[]): HTMLElement;
}

Event Triggering Interface

Built-in Mock Utilities

Aurelia's testing package provides sophisticated mock utilities for common testing scenarios.

Available Mock Classes

Using Built-in Mocks

Spy Subscriber for Observable Testing

Advanced Assertion Patterns

Custom Assertion Helpers

Using Custom Assertions

Complex Event Simulation

Drag and Drop Testing

Keyboard Navigation Testing

Form Testing Patterns

Complex Form Validation

Multi-step Form Testing

Performance and Memory Testing

Render Performance Testing

Memory Leak Detection

Integration Testing Patterns

Multi-Component Integration

Service Integration Testing

Async Testing Advanced Patterns

Testing Race Conditions

Testing Timeout Scenarios

Best Practices Summary

1. Test Organization

2. Assertion Strategies

  • Use the most specific assertion available

  • Create custom assertions for domain-specific validations

  • Test both positive and negative scenarios

  • Verify side effects and state changes

3. Mock Management

  • Use real implementations for integration tests

  • Mock external dependencies and slow operations

  • Prefer built-in mocks when available

  • Clean up mock state between tests

4. Async Handling

  • Always await startPromise before assertions

  • Use tasksSettled() for complex async operations

  • Test timeout and error scenarios

  • Handle race conditions explicitly

5. Performance Considerations

  • Monitor render times for large datasets

  • Test memory usage patterns

  • Verify cleanup prevents memory leaks

  • Use performance budgets in CI/CD

This advanced testing guide provides comprehensive patterns for testing sophisticated Aurelia 2 applications. Combine these techniques with the foundational testing guides to create robust, maintainable test suites that ensure application quality and reliability.

Last updated

Was this helpful?