@frontend-testing/vitest-browser-navigate

中文文档
0.0.1-alpha.8发布于 2026/01/02MITTypeScriptESM + CJS零依赖近 30 天下载 3701

Browser navigation command for Vitest browser mode

原始使用文档
文档目录10 节
  1. Features
  2. Installation
  3. Quick Start
  4. 1. Configure Vitest
  5. 2. Use in Tests
  6. TypeScript Support
  7. API
  8. commands.navigate(path: string): Promise<void>
  9. How It Works
  10. License

@frontend-testing/vitest-browser-navigate

Browser navigation command for Vitest browser mode. Simulates SPA navigation by updating browser history and dispatching popstate events.

Features

  • 🧭 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

Installation

npm install -D @frontend-testing/vitest-browser-navigate

Peer Dependencies: vitest >= 4.0.0

Quick Start

1. Configure Vitest

// 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 },
    },
  },
});

2. Use in Tests

import { commands } from "vitest/browser";

describe("MyPage", () => {
  it("navigates to dashboard", async () => {
    await commands.navigate("/dashboard");
    expect(window.location.pathname).toBe("/dashboard");
  });
});

TypeScript Support

Types for commands.navigate() are included automatically. If your IDE doesn't recognize the types, add a side-effect import:

Option A: In your test setup file (recommended)

// src/test/setup.ts
import "@frontend-testing/vitest-browser-navigate";
// ... rest of your setup

Option B: In individual test files

// my-component.spec.tsx
import "@frontend-testing/vitest-browser-navigate";
import { commands } from "vitest/browser";

// Now commands.navigate() has full type support
await commands.navigate("/dashboard");

Note: After adding the import, you may need to restart your TypeScript server (Cmd+Shift+P → "TypeScript: Restart TS Server").

API

commands.navigate(path: string): Promise<void>

Navigates to the specified path by:

  1. Calling window.history.pushState()
  2. Dispatching a popstate event (triggers SPA routers like React Router)
  3. 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");

How It Works

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.

License

MIT

版本与发布记录

@frontend-testing/vitest-browser-navigate 共发布 5 个版本,首次发布于 2025/12/13;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.0.1-alpha.8latest2026/01/0210 个月前
0.0.1-alpha.72026/01/0210 个月前
0.0.1-alpha.62025/12/1311 个月前
0.0.1-alpha.1alpha2025/12/1311 个月前
0.0.1-alpha.02025/12/1311 个月前

关于 @frontend-testing/vitest-browser-navigate 的常见问题

@frontend-testing/vitest-browser-navigate 是什么?

Browser navigation command for Vitest browser mode

如何安装 @frontend-testing/vitest-browser-navigate?

在项目目录运行 npm install @frontend-testing/vitest-browser-navigate,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vitest(>=4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@frontend-testing/vitest-browser-navigate 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frontend-testing/vitest-browser-navigate 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@frontend-testing/vitest-browser-navigate 可以商用吗?

@frontend-testing/vitest-browser-navigate 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@frontend-testing/vitest-browser-navigate 的下载量有多少?

根据 npm 官方下载统计,@frontend-testing/vitest-browser-navigate 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,701 次,日均约 123 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frontend-testing/vitest-browser-navigate 还在维护吗?

最新版本 0.0.1-alpha.8 发布于 2026/01/02(10 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2025/12/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。