Testing guide
Learn how to test routed components, navigation flows, and router events in Aurelia applications.
1. Minimal setup: createFixture + RouterConfiguration
createFixture + RouterConfigurationimport { customElement } from '@aurelia/runtime-html';
import { createFixture, assert } from '@aurelia/testing';
import { RouterConfiguration, ICurrentRoute, IRouter, IRouteViewModel, Params, route } from '@aurelia/router';
@customElement({ name: 'home-page', template: 'Home' })
class HomePage {}
@customElement({ name: 'user-page', template: 'User ${id}' })
class UserPage implements IRouteViewModel {
id = '';
canLoad(params: Params) {
this.id = params.id ?? '';
return true;
}
}
@route({
routes: [
{ path: ['', 'home'], component: HomePage },
{ path: 'users/:id', component: UserPage },
],
})
class App {}
describe('router', function () {
it('navigates and updates ICurrentRoute', async function () {
const { appHost, container, startPromise, stop } = createFixture(
'<au-viewport></au-viewport>',
App,
[RouterConfiguration.customize({ historyStrategy: 'none' })],
);
await startPromise;
const router = container.get(IRouter);
const currentRoute = container.get(ICurrentRoute);
// Initial navigation loads the default route (empty path).
assert.html.textContent(appHost, 'Home');
await router.load('users/42');
assert.html.textContent(appHost, 'User 42');
// `currentRoute.path` is an instruction path (no leading '/').
assert.strictEqual(currentRoute.path, 'users/42');
await stop(true);
});
});2. Testing guard outcomes (canLoad / canUnload)
canLoad / canUnload)3. Testing router events (IRouterEvents)
IRouterEvents)4. When to mock (and what to mock)
Last updated
Was this helpful?