Skip to content

Commit f957b6c

Browse files
committed
docs: update env & test-module
1 parent 5fa1b37 commit f957b6c

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

advanced/api/test-module.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
`TestModule` 实例始终具有一个 `type` 属性,其值为 `module`。你可以使用它来区分不同的任务类型:
66

77
```ts
8-
if (task.type === 'module') {
9-
task // TestModule
8+
if (task.type === "module") {
9+
task; // TestModule
1010
}
1111
```
1212

@@ -19,50 +19,50 @@ if (task.type === 'module') {
1919
这通常是一个绝对的 Unix 文件路径(即使在 Windows 上也是如此)。如果文件不在磁盘上,它可以是一个虚拟 ID。此值对应于 Vite 的 `ModuleGraph` ID。
2020

2121
```ts
22-
'C:/Users/Documents/project/example.test.ts' //
23-
'/Users/mac/project/example.test.ts' //
24-
'C:\\Users\\Documents\\project\\example.test.ts' //
22+
"C:/Users/Documents/project/example.test.ts"; //
23+
"/Users/mac/project/example.test.ts"; //
24+
"C:\\Users\\Documents\\project\\example.test.ts"; //
2525
```
2626

2727
## relativeModuleId
2828

29-
Module id relative to the project. This is the same as `task.name` in the deprecated API.
29+
相对于项目的模块 ID。这与已弃用 API 中的 `task.name` 相同。
3030

3131
```ts
32-
'project/example.test.ts' //
33-
'example.test.ts' //
34-
'project\\example.test.ts' //
32+
"project/example.test.ts"; //
33+
"example.test.ts"; //
34+
"project\\example.test.ts"; //
3535
```
3636

3737
## state
3838

3939
```ts
40-
function state(): TestModuleState
40+
function state(): TestModuleState;
4141
```
4242

4343
[`testSuite.state()`](/advanced/api/test-suite#state) 的工作方式相同,但如果模块尚未执行,还可以返回 `queued`
4444

4545
## meta <Version>3.1.0</Version> {#meta}
4646

4747
```ts
48-
function meta(): TaskMeta
48+
function meta(): TaskMeta;
4949
```
5050

5151
在模块执行或收集过程中附加到模块的自定义[元数据](/advanced/metadata)。在测试运行期间,可以通过向 `task.meta` 对象分配属性来附加 meta:
5252

5353
```ts {5,10}
54-
import { test } from 'vitest'
55-
56-
describe('the validation works correctly', (task) => {
57-
// assign "decorated" during collection
58-
task.file.meta.decorated = false
59-
60-
test('some test', ({ task }) => {
61-
// assign "decorated" during test run, it will be available
62-
// only in onTestCaseReady hook
63-
task.file.meta.decorated = false
64-
})
65-
})
54+
import { test } from "vitest";
55+
56+
describe("the validation works correctly", (task) => {
57+
// 在收集阶段分配 "decorated"
58+
task.file.meta.decorated = false;
59+
60+
test("some test", ({ task }) => {
61+
// 在测试运行期间分配 "decorated",它将可用
62+
// 仅在 onTestCaseReady 钩子中
63+
task.file.meta.decorated = false;
64+
});
65+
});
6666
```
6767

6868
:::tip
@@ -72,51 +72,51 @@ describe('the validation works correctly', (task) => {
7272
## diagnostic
7373

7474
```ts
75-
function diagnostic(): ModuleDiagnostic
75+
function diagnostic(): ModuleDiagnostic;
7676
```
7777

7878
关于模块的有用信息,例如持续时间、内存使用等。如果模块尚未执行,所有诊断值将返回 `0`
7979

8080
```ts
8181
interface ModuleDiagnostic {
8282
/**
83-
* The time it takes to import and initiate an environment.
83+
* 导入和初始化环境所需的时间。
8484
*/
85-
readonly environmentSetupDuration: number
85+
readonly environmentSetupDuration: number;
8686
/**
87-
* The time it takes Vitest to setup test harness (runner, mocks, etc.).
87+
* Vitest 设置测试运行环境(运行器、模拟等)所需的时间。
8888
*/
89-
readonly prepareDuration: number
89+
readonly prepareDuration: number;
9090
/**
91-
* The time it takes to import the test module.
92-
* This includes importing everything in the module and executing suite callbacks.
91+
* 导入测试模块所需的时间。
92+
* 这包括导入模块中的所有内容以及执行套件回调函数。
9393
*/
94-
readonly collectDuration: number
94+
readonly collectDuration: number;
9595
/**
96-
* The time it takes to import the setup module.
96+
* 导入设置模块所需的时间。
9797
*/
98-
readonly setupDuration: number
98+
readonly setupDuration: number;
9999
/**
100-
* Accumulated duration of all tests and hooks in the module.
100+
* 模块中所有测试和钩子函数的累计持续时间。
101101
*/
102-
readonly duration: number
102+
readonly duration: number;
103103
/**
104-
* The amount of memory used by the module in bytes.
105-
* This value is only available if the test was executed with `logHeapUsage` flag.
104+
* 模块使用的内存量(以字节为单位)。
105+
* 此值仅在使用 `logHeapUsage` 标志执行测试时才可用。
106106
*/
107-
readonly heap: number | undefined
107+
readonly heap: number | undefined;
108108
/**
109-
* The time spent importing every non-externalized dependency that Vitest has processed.
109+
* Vitest处理的每个非外部化依赖项的导入时间。
110110
*/
111-
readonly importDurations: Record<string, ImportDuration>
111+
readonly importDurations: Record<string, ImportDuration>;
112112
}
113113

114-
/** The time spent importing & executing a non-externalized file. */
114+
/** 导入和执行非外部化文件所花费的时间。 */
115115
interface ImportDuration {
116-
/** The time spent importing & executing the file itself, not counting all non-externalized imports that the file does. */
117-
selfTime: number
116+
/** 导入和执行文件本身所花费的时间,不包括该文件所依赖的所有非外部化导入。 */
117+
selfTime: number;
118118

119-
/** The time spent importing & executing the file and all its imports. */
120-
totalTime: number
119+
/** 导入和执行文件及其所有导入项所花费的时间。 */
120+
totalTime: number;
121121
}
122122
```

guide/environment.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Vitest 提供 [`environment`](/config/#environment) 选项以在特定环境中
1616
::: info
1717
当使用 `jsdom``happy-dom` 环境时,Vitest 在导入 [CSS](https://vitejs.dev/guide/features.html#css)[资源文件](https://vitejs.dev/guide/features.html#static-assets) 时遵循与 Vite 相同的规则。如果在导入外部依赖时出现 `unknown extension .css` 错误,则需要通过将所有相关包添加到 [`server.deps.inline`](/config/#server-deps-inline) 中,手动内联整个导入链。例如,在以下导入链中:`源代码 -> package-1 -> package-2 -> package-3`,如果错误发生在 `package-3`,你需要将这三个包都添加到 `server.deps.inline` 中。
1818

19-
外部依赖中的CSS和资源文件的 `require` 调用会自动解析。
19+
外部依赖中的 CSS 和资源文件的 `require` 调用会自动解析。
2020
:::
2121

2222
::: warning
@@ -32,45 +32,45 @@ Vitest 并不将 `browser` 视作一种测试环境。如果你想让部分测
3232
```ts
3333
// @vitest-environment jsdom
3434

35-
import { expect, test } from 'vitest'
35+
import { expect, test } from "vitest";
3636

37-
test('test', () => {
38-
expect(typeof window).not.toBe('undefined')
39-
})
37+
test("test", () => {
38+
expect(typeof window).not.toBe("undefined");
39+
});
4040
```
4141

4242
## 自定义环境 {#custom-environment}
4343

4444
你可以创建自己的包来扩展 Vitest 环境。为此,请创建一个名为 `vitest-environment-${name}` 的包,或者指定一个有效的 JS/TS 文件路径。该包应该导出一个形状为 `Environment` 的对象。
4545

4646
```ts
47-
import type { Environment } from 'vitest/environments'
47+
import type { Environment } from "vitest/environments";
4848

4949
export default <Environment>{
50-
name: 'custom',
51-
viteEnvironment: 'ssr',
50+
name: "custom",
51+
viteEnvironment: "ssr",
5252
// optional - only if you support "experimental-vm" pool
5353
async setupVM() {
54-
const vm = await import('node:vm')
55-
const context = vm.createContext()
54+
const vm = await import("node:vm");
55+
const context = vm.createContext();
5656
return {
5757
getVmContext() {
58-
return context
58+
return context;
5959
},
6060
teardown() {
61-
// called after all tests with this env have been run
61+
// 在所有使用此环境的测试运行完毕后调用
6262
},
63-
}
63+
};
6464
},
6565
setup() {
66-
// custom setup
66+
// 自定义设置
6767
return {
6868
teardown() {
69-
// called after all tests with this env have been run
69+
// 在所有使用此环境的测试运行完毕后调用
7070
},
71-
}
71+
};
7272
},
73-
}
73+
};
7474
```
7575

7676
::: warning
@@ -80,30 +80,30 @@ Vitest 要求环境对象显式提供 `viteEnvironment` 字段(若省略则取
8080
你还可以通过 `vitest/environments` 访问默认的 Vitest 环境:
8181

8282
```ts
83-
import { builtinEnvironments, populateGlobal } from 'vitest/environments'
83+
import { builtinEnvironments, populateGlobal } from "vitest/environments";
8484

85-
console.log(builtinEnvironments) // { jsdom, happy-dom, node, edge-runtime }
85+
console.log(builtinEnvironments); // { jsdom, happy-dom, node, edge-runtime }
8686
```
8787

8888
Vitest 还提供了 `populateGlobal` 实用函数,可用于将属性从对象移动到全局命名空间:
8989

9090
```ts
9191
interface PopulateOptions {
92-
// should non-class functions be bind to the global namespace
93-
bindFunctions?: boolean
92+
// 非类函数是否应该绑定到全局命名空间
93+
bindFunctions?: boolean;
9494
}
9595

9696
interface PopulateResult {
97-
// a list of all keys that were copied, even if value doesn't exist on original object
98-
keys: Set<string>
99-
// a map of original object that might have been overridden with keys
100-
// you can return these values inside `teardown` function
101-
originals: Map<string | symbol, any>
97+
// 所有被复制的键的列表,即使原始对象上不存在该值
98+
keys: Set<string>;
99+
// 可能已被键覆盖的原始对象的映射
100+
// 你可以在 `teardown` 函数中返回这些值
101+
originals: Map<string | symbol, any>;
102102
}
103103

104104
export function populateGlobal(
105105
global: any,
106106
original: any,
107107
options: PopulateOptions
108-
): PopulateResult
108+
): PopulateResult;
109109
```

0 commit comments

Comments
 (0)