Testing components in Aurelia 2 is a straightforward process thanks to the framework's design and the utilities provided by the @aurelia/testing package. This guide will walk you through the steps to test your components effectively, ensuring they work as expected within the context of a view.
In Aurelia, a component typically consists of a view (HTML) and a view model (JavaScript or TypeScript). To ensure the quality and correctness of your components, you should write tests that cover both aspects. Testing components involves checking that the view renders correctly with given data and that the view model behaves as intended when interacting with the view.
Testing Strategy
When testing components, we will focus on integration tests that involve both the view and view model. This approach allows us to verify the component as a whole, as it would function within an Aurelia application.
Example Component
For demonstration purposes, we will use a simple PersonDetail component with bindable properties name and age.
<template><p>Person is called ${name} and is ${age} years old.</p></template>
Writing the Test
We aim to test that the PersonDetail component renders the expected text when provided with name and age properties.
Test Setup
Before writing the test, ensure your environment is correctly set up for testing. Refer to the Overview section for details on how to initialize the Aurelia testing platform.
Test Implementation
Create a test file for your component, such as person-detail.spec.ts, and implement your tests using the syntax of your chosen test runner. The following example uses Jest:
In this example, createFixture is used to instantiate the component with a test context, binding name and age to specified values. We then assert that the component's text content includes the correct information. After the test completes, stop(true) cleans up the component instance to avoid memory leaks and ensure test isolation.
Testing Components with Dependencies
If your component has dependencies, such as services or other custom elements, you'll need to register these within the Aurelia testing container.
Example with a Dependency
Assume PersonDetail depends on a PersonFormatter service:
To test this component, you can create a mock PersonFormatter and register it with the Aurelia container:
In the test above, we use Jest's jest.fn() to create a mock implementation of PersonFormatter. We then verify that the mock's format method is called with the correct arguments and that the component's text content includes the formatted details.
Advanced Testing Patterns
Testing Component Lifecycle
Components have lifecycle hooks that can be tested to ensure proper behavior:
Test the lifecycle hooks:
Testing Component Events and Communication
Test components that emit custom events:
Test event publishing:
Testing Async Operations
Test components with async operations using task queue utilities:
Test async operations:
Testing State Management Integration
Test components that use state management:
Test state bindings:
Testing Conditional Rendering
Test components with conditional rendering logic:
With template:
Test conditional rendering:
Testing Component Performance
Test component render performance and memory usage:
Best Practices for Advanced Testing
1. Test Organization
Group related tests using describe blocks
Use descriptive test names that explain the behavior being tested
Follow the AAA pattern: Arrange, Act, Assert
2. Async Testing
Always use await with startPromise and stop(true)
Use tasksSettled() when testing async operations
Handle promise rejections properly in tests
Note: tearDown() is deprecated, use stop(true) instead
3. Mocking Strategies
Mock external dependencies using Registration.instance from @aurelia/kernel
Use Jest spies to verify method calls
Mock only what's necessary for the test
Register mocks in the fourth parameter of createFixture
Testing Aurelia components involves setting up a test environment, creating fixtures, and writing assertions based on your expectations. By following these patterns and best practices, you can ensure that your components are reliable, performant, and maintainable. Remember to clean up after your tests to maintain a clean test environment and to avoid any side effects between tests.
The patterns shown here cover lifecycle testing, event handling, async operations, state management, conditional rendering, and basic performance testing. For more advanced testing techniques and comprehensive API coverage, refer to the Advanced Testing guide.
Complete Fixture API Reference
The createFixture function returns a comprehensive fixture object with many utility methods for testing:
import { createFixture } from '@aurelia/testing';
import { PersonDetail } from './person-detail';
import { bootstrapTestEnvironment } from './path-to-your-initialization-code';
describe('PersonDetail component', () => {
beforeAll(() => {
// Initialize the test environment before running the tests
bootstrapTestEnvironment();
});
it('renders the name and age correctly', async () => {
const { appHost, startPromise, stop } = createFixture(
'<person-detail name.bind="testName" age.bind="testAge"></person-detail>',
class App {
testName = 'Alice';
testAge = 30;
},
[PersonDetail]
);
await startPromise;
expect(appHost.textContent).toContain('Person is called Alice and is 30 years old.');
// Use stop(true) instead of deprecated tearDown()
await stop(true);
});
// Additional tests...
});
import { resolve } from 'aurelia';
import { PersonFormatter } from './person-formatter';
export class PersonDetail {
private personFormatter = resolve(PersonFormatter);
@bindable name: string;
@bindable age: number;
get formattedDetails() {
return this.personFormatter.format(this.name, this.age);
}
}
import { createFixture } from '@aurelia/testing';
import { Registration } from 'aurelia';
import { PersonDetail } from './person-detail';
import { PersonFormatter } from './person-formatter';
describe('PersonDetail with PersonFormatter dependency', () => {
it('formats the details using PersonFormatter', async () => {
const mockPersonFormatter = {
format: jest.fn().mockImplementation((name, age) => `Formatted: ${name}, age ${age}`),
};
const { appHost, startPromise, stop } = createFixture(
'<person-detail name.bind="testName" age.bind="testAge"></person-detail>',
class App {
testName = 'Bob';
testAge = 40;
},
[PersonDetail],
[Registration.instance(PersonFormatter, mockPersonFormatter)]
);
await startPromise;
expect(mockPersonFormatter.format).toHaveBeenCalledWith('Bob', 40);
expect(appHost.textContent).toContain('Formatted: Bob, age 40');
await stop(true);
});
});
import { tasksSettled } from '@aurelia/runtime';
describe('ConditionalComponent', () => {
it('shows content based on conditions', async () => {
const { appHost, component, startPromise, stop } = createFixture(
'<conditional-component show-content.bind="show" items.bind="itemList"></conditional-component>',
class App {
show = false;
itemList: any[] = [];
},
[ConditionalComponent]
);
await startPromise;
// Initially hidden
expect(appHost.textContent).not.toContain('Found');
expect(appHost.textContent).not.toContain('No items');
// Show content with no items - modify the app component, not the child
component.show = true;
await tasksSettled();
expect(appHost.textContent).toContain('No items found');
// Add items
component.itemList = [1, 2, 3];
await tasksSettled();
expect(appHost.textContent).toContain('Found 3 items');
await stop(true);
});
});
describe('PerformanceComponent', () => {
it('renders within acceptable time limits', async () => {
const startTime = performance.now();
const { startPromise, stop } = createFixture(
'<performance-component items.bind="largeDataSet"></performance-component>',
class App {
largeDataSet = Array.from({ length: 1000 }, (_, i) => ({ id: i, value: `Item ${i}` }));
},
[PerformanceComponent]
);
await startPromise;
const renderTime = performance.now() - startTime;
expect(renderTime).toBeLessThan(100); // Should render in under 100ms
await stop(true);
});
it('does not leak memory on repeated renders', async () => {
const initialMemory = (performance as any).memory?.usedJSHeapSize || 0;
for (let i = 0; i < 10; i++) {
const { startPromise, stop } = createFixture(
'<performance-component></performance-component>',
class App {},
[PerformanceComponent]
);
await startPromise;
await stop(true);
}
// Force garbage collection if available
if ((global as any).gc) {
(global as any).gc();
}
const finalMemory = (performance as any).memory?.usedJSHeapSize || 0;
const memoryIncrease = finalMemory - initialMemory;
// Memory increase should be minimal
expect(memoryIncrease).toBeLessThan(1024 * 1024); // Less than 1MB
});
});
// Get a single element (throws if multiple or none found)
const button = fixture.getBy('button');
const input = fixture.getBy<HTMLInputElement>('input[type="text"]');
// Get all matching elements
const allButtons = fixture.getAllBy('button');
// Get single element or null (throws if multiple found)
const optionalElement = fixture.queryBy('.optional-class');
// Text content assertions
fixture.assertText('Expected text content'); // Whole app
fixture.assertText('h1', 'Expected heading'); // Specific element
fixture.assertTextContain('partial text'); // Contains check
// HTML content assertions
fixture.assertHtml('<p>Expected HTML</p>');
fixture.assertHtml('.content', '<span>Expected</span>');
// Attribute assertions
fixture.assertAttr('button', 'disabled', null); // No disabled attribute
fixture.assertAttr('input', 'value', 'test-value');
fixture.assertAttrNS('svg', 'http://www.w3.org/2000/svg', 'viewBox', '0 0 100 100');
// CSS class assertions
fixture.assertClass('button', 'btn', 'btn-primary'); // Has these classes
fixture.assertClassStrict('button', 'btn', 'btn-primary'); // Has only these classes
// Style assertions
fixture.assertStyles('div', { color: 'red', fontSize: '16px' });
// Form element assertions
fixture.assertValue('input', 'expected value');
fixture.assertChecked('input[type="checkbox"]', true);