Section 15 of 17
Testing per library type
Contents
Each type has a natural kind of test, and the boundaries make each one cheap.
| Type | Test | Speed | What it fakes |
|---|---|---|---|
model, util |
Plain unit tests, no framework | milliseconds | nothing |
data-access reducers, selectors |
Pure function tests | milliseconds | nothing |
data-access effects, queries and mutations |
Tests with the network faked by MSW | fast | HTTP |
| facade | Integration with the real store | fast | HTTP (MSW) |
ui |
Component tests, stories in Storybook | fast | nothing, it has only inputs |
feature |
Component test with a fake facade | fast | the facade |
| app | End-to-end with Playwright, a few flows | slow | the back-end, or nothing |
A reducer test needs no TestBed:
import { describe, expect, it } from 'vitest';
import { workOrdersApi, workOrdersPage } from './work-orders.events';
import { workOrdersFeature } from './work-orders.feature';
import { aWorkOrder } from '@fleetline/maintenance/model/testing';
describe('work orders reducer', () => {
it('puts the previous mechanic back when an assignment fails', () => {
const order = aWorkOrder({ id: 'wo-1', assigneeId: 'mech-1' });
const loaded = workOrdersFeature.reducer(undefined, workOrdersApi.loadSucceeded({ workOrders: [order] }));
const optimistic = workOrdersFeature.reducer(
loaded,
workOrdersPage.assigneeChosen({ id: 'wo-1', assigneeId: 'mech-2', previousAssigneeId: 'mech-1' }),
);
const failed = workOrdersFeature.reducer(
optimistic,
workOrdersApi.assignFailed({ id: 'wo-1', previousAssigneeId: 'mech-1', error: 'conflict' }),
);
expect(optimistic.entities['wo-1']?.assigneeId).toBe('mech-2');
expect(failed.entities['wo-1']?.assigneeId).toBe('mech-1');
expect(failed.error).toBe('conflict');
});
});
A feature test provides a fake facade, because the component sees nothing else:
import { signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { expect, it, vi } from 'vitest';
import { WorkOrdersFacade } from '@fleetline/maintenance/data-access';
import { VehicleSummaryStore } from '@fleetline/fleet/api';
import { aWorkOrder } from '@fleetline/maintenance/model/testing';
import { WorkOrdersPageComponent } from './work-orders-page.component';
it('assigns the chosen mechanic', async () => {
const order = aWorkOrder({ id: 'wo-1' });
const facade = {
visible: signal([order]),
overdueCount: signal(0),
filter: signal({ depotId: 'd-1', status: 'all' as const, query: '' }),
loading: signal(false),
error: signal(null),
open: vi.fn(),
filterBy: vi.fn(),
assign: vi.fn(),
};
TestBed.configureTestingModule({
providers: [
{ provide: WorkOrdersFacade, useValue: facade },
{ provide: VehicleSummaryStore, useValue: { entityMap: signal({}), ensureLoaded: vi.fn() } },
],
});
const fixture = TestBed.createComponent(WorkOrdersPageComponent);
fixture.componentRef.setInput('depotId', 'd-1');
await fixture.whenStable();
fixture.debugElement.query((el) => el.name === 'mnt-work-order-card').triggerEventHandler('assign', 'mech-7');
expect(facade.open).toHaveBeenCalledWith('d-1');
expect(facade.assign).toHaveBeenCalledWith(order, 'mech-7');
});
In React and Vue the facade does not come through DI, so the feature test replaces the module. The principle is the same: the component sees nothing but the facade and the ui libraries.
libs/maintenance/feature-work-orders/src/lib/work-orders-page.spec.tsx
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { expect, it, vi } from 'vitest';
import { useWorkOrders } from '@fleetline/maintenance/data-access';
import { aWorkOrder } from '@fleetline/maintenance/model/testing';
import { WorkOrdersPage } from './work-orders-page';
vi.mock('@fleetline/maintenance/data-access', () => ({ useWorkOrders: vi.fn() }));
vi.mock('@fleetline/fleet/api', () => ({ useVehicleSummaries: () => ({}) }));
vi.mock('@fleetline/maintenance/ui-work-order-card', () => ({
StatusFilter: () => null,
WorkOrderCard: ({ onAssign }: { onAssign: (assigneeId: string) => void }) => (
<button type="button" onClick={() => onAssign('mech-7')}>
assign
</button>
),
}));
it('assigns the chosen mechanic', async () => {
const order = aWorkOrder({ id: 'wo-1' });
const assign = vi.fn();
vi.mocked(useWorkOrders).mockReturnValue({
visible: [order],
overdueCount: 0,
filter: { depotId: 'd-1', status: 'all', query: '' },
loading: false,
error: null,
filterBy: vi.fn(),
assign,
});
render(<WorkOrdersPage depotId="d-1" />);
await userEvent.click(screen.getByRole('button', { name: 'assign' }));
expect(useWorkOrders).toHaveBeenCalledWith('d-1');
expect(assign).toHaveBeenCalledWith(order, 'mech-7');
});
In Vue the same test is written with @vue/test-utils and the same vi.mock, and a component with await in <script setup> is mounted with mountSuspended from @nuxt/test-utils. In facade integration tests, createTestingPinia from @pinia/testing replaces the Pinia stores, and in all three frameworks MSW fakes the network, so the data-access tests do not depend on how the HTTP service gets its configuration.
Test data builders such as aWorkOrder live in a secondary entry point of model (@fleetline/maintenance/model/testing), so every framework's tests use the same ones.