From 004b7595f84a597f548d89bc57352036eaad3715 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 17 Dec 2025 13:22:43 +0100 Subject: [PATCH 1/3] docs: clarify task test context unavailability in file/worker-scoped fixtures (#9197) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com> --- guide/test-context.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/guide/test-context.md b/guide/test-context.md index 6e0e0da1..ad125dae 100644 --- a/guide/test-context.md +++ b/guide/test-context.md @@ -407,6 +407,12 @@ const test = baseTest.extend({ }) ``` +::: warning +The built-in [`task`](#task) test context is **not available** in file-scoped or worker-scoped fixtures. These fixtures receive a different context object (file or worker context) that does not include test-specific properties like `task`. + +If you need access to file-level metadata like the file path, use `expect.getState().testPath` instead. +::: + The `worker` scope will run the fixture once per worker. The number of running workers depends on various factors. By default, every file runs in a separate worker, so `file` and `worker` scopes work the same way. However, if you disable [isolation](/config/#isolate), then the number of workers is limited by the [`maxWorkers`](/config/#maxworkers) configuration. From 5493fcff8e077092a53aca95d367f08b822b2b95 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 17 Dec 2025 13:23:17 +0100 Subject: [PATCH 2/3] docs: update resolveSnapshotPath example to show context.config usage for projects (#9114) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com> Co-authored-by: Hiroshi Ogawa --- config/resolvesnapshotpath.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/config/resolvesnapshotpath.md b/config/resolvesnapshotpath.md index 981ff412..c63abfd5 100644 --- a/config/resolvesnapshotpath.md +++ b/config/resolvesnapshotpath.md @@ -19,3 +19,23 @@ export default defineConfig({ }, }) ``` + +You can also use the `context` parameter to access the project's serialized config. This is useful when you have multiple [projects](/guide/projects) and want to store snapshots in different locations based on the project name: + +```ts +import { basename, dirname, join } from 'node:path' +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + resolveSnapshotPath(testPath, snapExtension, context) { + return join( + dirname(testPath), + '__snapshots__', + context.config.name ?? 'default', + basename(testPath) + snapExtension, + ) + }, + }, +}) +``` From 7cd4a8fa17baa5fce6b9f4e83acb81eecfa385f0 Mon Sep 17 00:00:00 2001 From: noise Date: Thu, 18 Dec 2025 10:18:29 +0800 Subject: [PATCH 3/3] docs(cn): dissolve the conflict --- config/resolvesnapshotpath.md | 2 +- guide/test-context.md | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/config/resolvesnapshotpath.md b/config/resolvesnapshotpath.md index c63abfd5..0e1f1a24 100644 --- a/config/resolvesnapshotpath.md +++ b/config/resolvesnapshotpath.md @@ -2,7 +2,7 @@ title: resolveSnapshotPath | Config outline: deep --- - + # resolveSnapshotPath - **Type**: `(testPath: string, snapExtension: string, context: { config: SerializedConfig }) => string` diff --git a/guide/test-context.md b/guide/test-context.md index 22c0d0b2..2cd7e219 100644 --- a/guide/test-context.md +++ b/guide/test-context.md @@ -405,20 +405,16 @@ const test = baseTest.extend({ ], }) ``` + -<<<<<<< HEAD -`worker` 作用域将为每个工作线程运行一次夹具。运行的工作线程数量取决于各种因素。默认情况下,每个文件在单独的工作线程中运行,因此 `file` 和 `worker` 作用域的工作方式相同。 -======= ::: warning The built-in [`task`](#task) test context is **not available** in file-scoped or worker-scoped fixtures. These fixtures receive a different context object (file or worker context) that does not include test-specific properties like `task`. If you need access to file-level metadata like the file path, use `expect.getState().testPath` instead. ::: -The `worker` scope will run the fixture once per worker. The number of running workers depends on various factors. By default, every file runs in a separate worker, so `file` and `worker` scopes work the same way. ->>>>>>> 5493fcff8e077092a53aca95d367f08b822b2b95 +`worker` 作用域将为每个工作线程运行一次夹具。运行的工作线程数量取决于各种因素。默认情况下,每个文件在单独的工作线程中运行,因此 `file` 和 `worker` 作用域的工作方式相同。 - However, if you disable [isolation](/config/#isolate), then the number of workers is limited by the [`maxWorkers`](/config/#maxworkers) configuration. 请注意,在 `vmThreads` 或 `vmForks` 中运行测试时,指定 `scope: 'worker'` 的工作方式与 `scope: 'file'` 相同。这个限制存在是因为每个测试文件都有自己的 VM 上下文,所以如果 Vitest 只初始化一次,一个上下文可能会泄漏到另一个上下文中,并创建许多引用不一致的问题(例如,同一个类的实例会引用不同的构造函数)。