-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from vitest-dev/sync-83a0cab9-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 83a0cab
- Loading branch information
Showing
10 changed files
with
164 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,8 @@ onMounted(async () => { | |
</li> | ||
</template> | ||
|
||
<style> | ||
.flip { | ||
transform: rotateY(90deg); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Custom Pool | ||
|
||
::: warning | ||
这是高级 API。如果你只需要运行测试,你可能不需要这个。它主要被库的作者使用。 | ||
::: | ||
|
||
Vitest 在默认情况下以多种方式运行测试: | ||
|
||
- `threads` 使用 `node:worker_threads` 运行测试(通过新的 worker 上下文提供隔离) | ||
- `forks` 使用 `node:child_process` 运行测试(通过新的 `child_process.fork` 进程提供隔离) | ||
- `vmThreads` 使用 `node:worker_threads` 运行测试(但是通过 `vm` 模块而不是新的 worker 上下文提供隔离) | ||
- `browser` 使用浏览器提供程序运行测试 | ||
- `typescript` 在测试中运行类型检查 | ||
|
||
你可以通过指定文件路径来提供自己的池: | ||
|
||
```ts | ||
export default defineConfig({ | ||
test: { | ||
// 默认情况下,将使用自定义池运行每个文件 | ||
pool: './my-custom-pool.ts', | ||
// 可以使用 `poolOptions` 对象提供选项 | ||
poolOptions: { | ||
myCustomPool: { | ||
customProperty: true, | ||
}, | ||
}, | ||
// 还可以为文件子集指定池 | ||
poolMatchGlobs: [ | ||
['**/*.custom.test.ts', './my-custom-pool.ts'], | ||
], | ||
}, | ||
}) | ||
``` | ||
|
||
## API | ||
|
||
在 `pool` 选项中指定的文件应该导出一个函数(可以是异步的),该函数接受 `Vitest` 接口作为其第一个选项。这个函数需要返回一个与 `ProcessPool` 接口匹配的对象: | ||
|
||
```ts | ||
import { ProcessPool, WorkspaceProject } from 'vitest/node' | ||
|
||
export interface ProcessPool { | ||
name: string | ||
runTests: (files: [project: WorkspaceProject, testFile: string][], invalidates?: string[]) => Promise<void> | ||
close?: () => Promise<void> | ||
} | ||
``` | ||
|
||
这个函数只会被调用一次(除非服务器配置被更新),通常最好在这个函数内初始化测试所需的一切,并在调用 `runTests` 时重复使用它。 | ||
|
||
Vitest 在安排运行新测试时调用 `runTest`。如果 `files` 为空,将不会调用它。第一个参数是一个元组数组:第一个元素是对工作区项目的引用,第二个元素是测试文件的绝对路径。在调用 `runTests` 之前,文件将使用 [`sequencer`](/config/#sequence.sequencer) 进行排序。可能(但不太可能)会有相同的文件出现两次,但它们将始终属于不同的项目 - 这是通过 [`vitest.workspace.ts`](/guide/workspace) 配置实现的。 | ||
|
||
Vitest 会等到 `runTests` 执行完毕后才结束运行(即只有在 `runTests` 解决后才会触发 [`onFinished`](/guide/reporters))。 | ||
|
||
如果你正在使用自定义池,需要自行提供测试文件及其结果 - 可以参考 [`vitest.state`](https://github.com/vitest-dev/vitest/blob/feat/custom-pool/packages/vitest/src/node/state.ts)(最重要的是 `collectFiles` 和 `updateTasks`)。Vitest 使用 `@vitest/runner` 包中的 `startTests` 函数来执行这些操作。 | ||
|
||
要在不同进程之间进行通信,可以使用 `vitest/node` 中的 `createMethodsRPC` 创建方法对象,并使用你喜欢的任何通信形式。例如,要使用 `birpc` 的 websockets,可以编写类似以下的内容: | ||
|
||
```ts | ||
import { createBirpc } from 'birpc' | ||
import { parse, stringify } from 'flatted' | ||
import { WorkspaceProject, createMethodsRPC } from 'vitest/node' | ||
|
||
function createRpc(project: WorkspaceProject, wss: WebSocketServer) { | ||
return createBirpc( | ||
createMethodsRPC(project), | ||
{ | ||
post: msg => wss.send(msg), | ||
on: fn => wss.on('message', fn), | ||
serialize: stringify, | ||
deserialize: parse, | ||
}, | ||
) | ||
} | ||
``` | ||
|
||
为了确保收集每个测试,您可以调用 `ctx.state.collectFiles` 并将其交给 Vitest 报告器: | ||
|
||
```ts | ||
async function runTests(project: WorkspaceProject, tests: string[]) { | ||
// ... 运行测试,放入 `files` 和 `tasks` 中 | ||
const methods = createMethodsRPC(project) | ||
await methods.onCollected(files) | ||
// 大多数报告都依赖于在 `onTaskUpdate` 中更新结果 | ||
await methods.onTaskUpdate(tasks) | ||
} | ||
``` | ||
|
||
可以在 [pool/custom-pool.ts](https://github.com/vitest-dev/vitest/blob/feat/custom-pool/test/run/pool-custom-fixtures/pool/custom-pool.ts). 中看到一个简单的示例。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.