diff --git a/api/advanced/runner.md b/api/advanced/runner.md index b820da6c..ab31176a 100644 --- a/api/advanced/runner.md +++ b/api/advanced/runner.md @@ -98,7 +98,7 @@ export interface VitestRunner { /** * 公开可用的配置。 */ - config: VitestRunnerConfig + config: SerializedConfig /** * 当前池的名称。可能会影响服务器端如何推断堆栈跟踪。 */ diff --git a/api/advanced/vitest.md b/api/advanced/vitest.md index 4f456a71..0749af20 100644 --- a/api/advanced/vitest.md +++ b/api/advanced/vitest.md @@ -27,7 +27,8 @@ Since Vitest 5, this property is always `'test'`. 公共 `state` 是一个实验性 API(除了 `vitest.state.getReportedEntity`)。破坏性更改可能不遵循 SemVer,请在使用时固定 Vitest 的版本。 ::: -全局状态存储有关当前测试的信息。默认情况下,它使用与 `@vitest/runner` 相同的 API,但我们建议通过调用 `@vitest/runner` API 上的 `state.getReportedEntity()` 来使用 [任务报告器 API](/api/advanced/reporters#reported-tasks): + +全局状态存储有关当前测试的信息。默认情况下,它使用内部可序列化的任务 API,但我们建议通过调用 `state.getReportedEntity()` 来使用 [任务报告器 API](/api/advanced/reporters#reported-tasks): ```ts const task = vitest.state.idMap.get(taskId) // 旧 API diff --git a/config/index.md b/config/index.md index 0fa77688..5663a913 100644 --- a/config/index.md +++ b/config/index.md @@ -9,6 +9,8 @@ outline: deep - 创建 `vitest.config.ts`,它将具有更高的优先级,并且会**覆盖** `vite.config.ts` 中的配置(Vitest 支持所有传统的 JS 和 TS 文件扩展名,但不支持 `json`) - 这意味着我们在 `vite.config` 中的所有选项将被**忽略**。 - 向 CLI 传递 `--config` 选项,例如 `vitest --config ./path/to/vitest.config.ts`。 - 使用 `process.env.VITEST` 或在 `defineConfig` 上的 `mode` 属性(如果没有用 `--mode` 覆盖,默认设置为 `test`)也可以在 `vite.config.ts` 中有条件地应用不同的配置。请注意,像任何其他环境变量一样,`VITEST` 也会在测试中的 `import.meta.env` 上暴露出来。 + +When an explicit `--config` option is not provided, Vitest looks for `vitest.config.{ts,mts,cts,js,mjs,cjs}` first and `vite.config.{ts,mts,cts,js,mjs,cjs}` second in the project [`root`](/config/root). If no config file is found, Vitest will run without one. 要配置 Vitest 本身,请在我们的 Vite 配置中添加 `test` 属性。如果我们是从 `vite` 本身导入 `defineConfig`,我们还需要在配置文件顶部使用 [三斜杠指令](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) 添加对 Vitest 类型引用。 diff --git a/guide/browser/why.md b/guide/browser/why.md index 57185e3b..5f55db81 100644 --- a/guide/browser/why.md +++ b/guide/browser/why.md @@ -24,7 +24,9 @@ outline: deep ### 早期开发 {#early-development} -Vitest 的浏览器模式功能仍处于早期开发阶段。因此,它可能尚未完全优化,可能存在一些尚未解决的错误或问题。建议用户使用独立的浏览器端测试运行程序(如 WebdriverIO、Cypress 或 Playwright)来增强 Vitest 浏览器体验。 +### 非完全替代品 {#not-a-drop-in-replacement} + +Vitest 的浏览器模式功能并不能完全替代独立的端到端测试运行器。建议用户使用独立的浏览器端测试运行程序(如 WebdriverIO、Cypress 或 Playwright)来增强 Vitest 浏览器体验。 ### 更长的初始化时间 {#longer-initialization} diff --git a/guide/learn/writing-tests.md b/guide/learn/writing-tests.md index e579ada4..a7e59acd 100644 --- a/guide/learn/writing-tests.md +++ b/guide/learn/writing-tests.md @@ -176,7 +176,7 @@ test.for([ }) ``` -测试名称中的占位符 `%i`、`%s` 和 `%f` 会被每行中对应的值替换,因此输出会显示 `add(1, 1) -> 2`、`add(1, 2) -> 3` 等。 +在上面的例子中,`%i` 占位符会被每个数据行中的整数值替换。Vitest 还支持其他类型的占位符,例如用于字符串的 `%s` 和用于浮点数的 `%f`。因此,测试运行器会生成诸如 `add(1, 1) -> 2`、`add(1, 2) -> 3` 和 `add(2, 1) -> 3` 这样的测试名称。 如果你的用例包含两个或三个以上的值,传递对象更具可读性。在名称中使用 `$property` 来插入字段: diff --git a/guide/migration.md b/guide/migration.md index f2283d1c..54d8b71f 100644 --- a/guide/migration.md +++ b/guide/migration.md @@ -125,6 +125,19 @@ await expect.element(banner).toMatchTextContent(/error/i) // [!code ++] await expect.element(banner).toHaveTextContent('Error!') ``` +### Config Files Are Not Looked Up From Parent Directories + +Vitest no longer searches parent directories for config files. If you previously relied on running `vitest` from a subdirectory while using a config file from a parent directory, pass the config explicitly and scope test discovery with `--dir`. For example, + +```bash +$ cd subdir && vitest # [!code --] +$ cd subdir && vitest --config ../vitest.config.ts # [!code ++] +``` + +### DOM Environment Global Assignments Now Update the Underlying Window + +Assignments to properties on `globalThis` or `window` in `jsdom` and `happy-dom` environments are now propagated to the underlying DOM implementation. Mutable properties such as `innerWidth` can affect APIs implemented by the DOM environment, for example `happy-dom`'s `matchMedia`. + ## 迁移至 Vitest 4.0 {#vitest-4} ::: warning 前提条件