Skip to content

Commit

Permalink
Merge pull request #343 from vitest-dev/sync-83a0cab9-1
Browse files Browse the repository at this point in the history
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 83a0cab
  • Loading branch information
elonehoo authored Nov 19, 2023
2 parents f1ad0f7 + 1f02adb commit 3812d26
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .vitepress/components/ListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ onMounted(async () => {
</li>
</template>

<style>
.flip {
transform: rotateY(90deg);
}
</style>
8 changes: 6 additions & 2 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default withPwa(
text: '高级 API',
items: [
{
text: 'Node API',
text: 'Vitest Node API',
link: '/advanced/api',
},
{
Expand All @@ -135,9 +135,13 @@ export default withPwa(
link: '/advanced/metadata',
},
{
text: 'Extending default reporters',
text: 'Extending Reporters',
link: '/advanced/reporters',
},
{
text: 'Custom Pool',
link: '/advanced/pool',
},
],
},
],
Expand Down
18 changes: 13 additions & 5 deletions .vitepress/contributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const plainTeamMembers: CoreTeam[] = [
discord: 'https://chat.antfu.me',
youtube: 'antfu',
sponsor: 'https://github.com/sponsors/antfu',
title: '狂热的开源者,在 NuxtLabs 工作',
title: '狂热的开源者,工作于',
org: 'NuxtLabs',
orgLink: 'https://nuxtlabs.com/',
desc: 'Vite 和 Vue 的核心团队成员',
Expand All @@ -81,8 +81,8 @@ const plainTeamMembers: CoreTeam[] = [
avatar: contributorsAvatars.AriPerkkio,
name: 'Ari Perkkiö',
github: 'AriPerkkio',
title: '全栈开发人员, 在 Cloudamite 工作',
desc: 'Vitest 团队成员',
title: '全栈开发人员, 工作于',
desc: 'Vitest 核心团队成员',
org: 'Cloudamite',
orgLink: 'https://cloudamite.com/',
},
Expand All @@ -93,7 +93,7 @@ const plainTeamMembers: CoreTeam[] = [
mastodon: 'https://elk.zone/m.webtoo.ls/@patak',
twitter: 'patak_dev',
sponsor: 'https://github.com/sponsors/patak-dev',
title: '二当家,在 StackBlitz 工作',
title: '二当家,工作于',
org: 'StackBlitz',
orgLink: 'https://stackblitz.com/',
desc: 'Vite 和 Vue 的核心团队成员',
Expand All @@ -107,6 +107,14 @@ const plainTeamMembers: CoreTeam[] = [
title: '全栈和安卓开发者',
desc: 'Vite 的狂热追随者',
},
{
avatar: contributorsAvatars.Dunqing,
name: 'Dunqing',
github: 'Dunqing',
twitter: '@Dunqingg',
title: '热衷于开源贡献者',
desc: 'Vitest 和 UnoCSS 团队成员',
},
{
avatar: contributorsAvatars.Aslemammad,
name: 'Mohammad Bagher',
Expand All @@ -132,7 +140,7 @@ const plainTeamMembers: CoreTeam[] = [
mastodon: 'https://elk.zone/hachyderm.io/@zx',
twitter: 'zxch3n',
title: '全栈开发人员',
desc: '创建协作工具',
desc: '开发 CRDT 和本地优先软件',
},
{
avatar: contributorsAvatars.poyoho,
Expand Down
7 changes: 2 additions & 5 deletions .vitepress/scripts/fetch-avatars.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { existsSync, promises as fsp } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join, resolve } from 'pathe'
import { teamMembers } from '../contributors'

const docsDir = resolve(dirname(fileURLToPath(import.meta.url)), '../..')

const pathContributors = resolve(docsDir, '.vitepress/contributor-names.json')
const dirAvatars = resolve(docsDir, 'public/user-avatars/')
const dirSponsors = resolve(docsDir, 'public/sponsors/')

let contributors: string[] = []

async function download(url: string, fileName: string) {
if (existsSync(fileName))
return
Expand All @@ -25,9 +23,8 @@ async function download(url: string, fileName: string) {
async function fetchAvatars() {
if (!existsSync(dirAvatars))
await fsp.mkdir(dirAvatars, { recursive: true })
contributors = JSON.parse(await fsp.readFile(pathContributors, { encoding: 'utf-8' }))

await Promise.all(contributors.map(name => download(`https://github.com/${name}.png?size=100`, join(dirAvatars, `${name}.png`))))
await Promise.all(teamMembers.map(c => c.github).map(name => download(`https://github.com/${name}.png?size=100`, join(dirAvatars, `${name}.png`))))
}

async function fetchSponsors() {
Expand Down
22 changes: 0 additions & 22 deletions .vitepress/style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ html:not(.dark) [img-dark] {

/* Overrides */

.vp-doc ul.features-list {
padding: 0;
}
.vp-doc ul.features-list li {
list-style: none;
display: flex;
gap: 0.4rem;
margin: 0;
}
.vp-doc ul.features-list li div:first-of-type {
position: relative;
}
.vp-doc ul.features-list li div:first-of-type div {
position: absolute;
}
.vp-doc ul.features-list li div:first-of-type div.flip {
transform: rotateY(90deg);
}
.vp-doc ul.features-list li div:first-of-type div div {
position: unset;
}

.sp .sp-link.link:hover,
.sp .sp-link.link:focus {
background-color: var(--vitest-c-sponsor-hover) !important;
Expand Down
90 changes: 90 additions & 0 deletions advanced/pool.md
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). 中看到一个简单的示例。
6 changes: 3 additions & 3 deletions advanced/reporters.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 扩展默认报告器
# 扩展默认报告器 (Extending Reporters)

你可以从 `vitest/reporters` 导入报告器并扩展它们以创建您的自定义报告器。

## 扩展内置报告器
## 扩展内置报告器 (Extending Built-in Reporters)

一般来说,你不需要从头开始创建报告器。`vitest` 附带了几个可以扩展的默认报告程序。

Expand Down Expand Up @@ -56,7 +56,7 @@ export default defineConfig({
})
```

## 导出报告器
## 导出报告器 (Exported Reporters)

`vitest` 附带了一些[内置报告器](/guide/reporters),你可以开箱即用。

Expand Down
2 changes: 1 addition & 1 deletion guide/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Mock Service Worker (MSW) 通过拦截测试发出的请求进行工作,允许
```js
import { afterAll, afterEach, beforeAll } from 'vitest'
import { setupServer } from 'msw/node'
import { HttpResponse, graphql, rest } from 'msw'
import { HttpResponse, graphql, http } from 'msw'

const posts = [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"unplugin-vue-components": "^0.25.2",
"vite": "^5.0.0-beta.15",
"vite-plugin-pwa": "^0.16.7",
"vitepress": "^1.0.0-rc.25",
"vitepress": "^1.0.0-rc.27",
"workbox-window": "^7.0.0"
}
}
Loading

0 comments on commit 3812d26

Please sign in to comment.