Browser navigation command for Vitest browser mode. Simulates SPA navigation by updating browser history and dispatching popstate events.
- 🧭 SPA Navigation - Navigate programmatically in browser tests
- 🎭 Playwright Support - Works with
@vitest/browser-playwright - 🚗 WebdriverIO Support - Works with
@vitest/browser-webdriverio - 📦 TypeScript - Full type support with automatic
commands.navigate()types
npm install -D @frontend-testing/vitest-browser-navigatePeer Dependencies: vitest >= 4.0.0
// vitest.config.ts
import { navigate } from "@frontend-testing/vitest-browser-navigate";
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
browser: {
enabled: true,
provider: "playwright", // or "webdriverio"
instances: [{ browser: "chromium" }],
commands: { navigate },
},
},
});import "@frontend-testing/vitest-browser-navigate"; // For types
import { commands } from "vitest/browser";
import { render } from "vitest-browser-react";
describe("MyPage", () => {
it("navigates to dashboard", async () => {
await commands.navigate("/dashboard");
await render(<App />);
expect(window.location.pathname).toBe("/dashboard");
});
});Navigates to the specified path by:
- Calling
window.history.pushState() - Dispatching a
popstateevent (triggers SPA routers like React Router) - Waiting for the browser to process the navigation
// Basic navigation
await commands.navigate("/login");
// With route parameters
await commands.navigate("/users/123");
// With query parameters
await commands.navigate("/search?q=test&page=1");The command executes inside the browser context and simulates navigation without a full page reload - exactly how SPA routers work. This makes it perfect for testing React Router, Vue Router, or any other client-side routing solution.
MIT