Testing
You will be able to test a route table’s match under Node and test page changes through <Router> in a browser. You will also know what to watch for when moving to a URL outside the route table and when checking history traversal.
On this page
Route table tests
src/routes.test.tsimport { expect, it } from 'vitest';
import { routes } from './routes';
it('sends /products/new to its own page', () => {
const found = routes.match('/products/new');
expect(found?.pattern).toBe('/products/new');
});
it('reads the id of a product page', () => {
const found = routes.match('/products/42');
expect(found?.params).toStrictEqual({ id: '42' });
});The match of the route table defineRoutes returns is a plain function that needs no browser. Which pattern a path matches, the params it yields, and the order patterns are tried in can all be checked by an ordinary test under Node.
matchPath is the same. Code that styles a link by the current path can take the path as an argument, and then it can be tested under Node too.
Tests in a browser
src/app.browser.test.tsximport { navigateTo, Router } from '@k8ordo/router';
import { expect, it } from 'vitest';
import { render } from 'vitest-browser-react';
import { routes } from './routes';
it('shows the product page once finished resolves', async () => {
await render(<Router routes={routes} />);
await navigateTo('/products/:id', { id: '1' }).finished;
Waits until the new page is on screen
const heading = document.querySelector('h1');
expect(heading?.textContent).toBe('Product 1');
});A test that renders <Router> and changes pages runs in a browser with the Navigation API. This package tests itself with Vitest’s browser mode on Chromium, Firefox and WebKit.
finished resolves once the new page is on screen. After awaiting it, the screen can be checked without retrying.
A page’s useEffect runs after the page is on screen. Check what an effect did with a form that retries until it passes, such as expect.element or vi.waitFor.
URLs outside the route table
src/app.browser.test.tsxconst interceptEverything = (event: NavigateEvent) => {
if (event.canIntercept) event.intercept();
};
const navigateOutside = async (url: string) => {
navigation.addEventListener('navigate', interceptEverything);
try {
await navigation.navigate(url, { history: 'replace' }).finished;
} finally {
navigation.removeEventListener('navigate', interceptEverything);
}
};
let home: string;
beforeEach(() => {
home = location.href;
});
afterEach(async () => {
await navigateOutside(home);
});A navigation <Router> does not handle becomes an ordinary page load. Inside a test, it navigates the page that runs the tests away from the runner.
Moving to a URL outside the route table before a test, and back to the original URL after it, are such navigations. For those only, the test adds a navigate listener and calls intercept() itself. When done, removeEventListener takes the listener off.
Note
Once <Router> is rendered, it handles navigations to paths in the route table. The test does not need to call intercept() for those.
History traversal
Vitest’s browser mode runs a test inside an iframe. There, Firefox and WebKit do not restore the scroll position on back and forward. Firefox also runs the traversal handler twice.
This package runs only its back and forward tests in a top-level page opened with Playwright. To check the scroll position after going back, do the same and test outside the iframe.