Skip to content

Commit 5fa1b37

Browse files
authored
Merge pull request #789 from lxKylin/test-context
refactor: update test-context
2 parents d87f5d9 + e4290db commit 5fa1b37

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

guide/test-context.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ outline: deep
1515
import { it } from 'vitest'
1616

1717
it('should work', ({ task }) => {
18-
// prints name of the testusage
18+
// 打印测试的名称
1919
console.log(task.name)
2020
})
2121
```
@@ -208,11 +208,11 @@ const test = baseTest.extend<{
208208
archive: []
209209
})
210210
211-
// todos will not run
211+
// todos 不会运行
212212
test('skip', () => {})
213213
test('skip', ({ archive }) => {})
214214
215-
// todos will run
215+
// todos 将会运行
216216
test('run', ({ todos }) => {})
217217
```
218218

@@ -241,12 +241,12 @@ import { test as base } from 'vitest'
241241
const test = base.extend({
242242
fixture: [
243243
async ({}, use) => {
244-
// this function will run
244+
// 这个函数将会运行
245245
setup()
246246
await use()
247247
teardown()
248248
},
249-
{ auto: true }, // Mark as an automatic fixture
249+
{ auto: true }, // 标记为自动装置
250250
],
251251
})
252252
@@ -255,25 +255,25 @@ test('works correctly')
255255

256256
#### 默认的装置 {#default-fixture}
257257

258-
Since Vitest 3, you can provide different values in different [projects](/guide/projects). To enable this feature, pass down `{ injected: true }` to the options. If the key is not specified in the [project configuration](/config/#provide), then the default value will be used.
258+
Vitest 3 开始,你可以在不同的[项目](/guide/projects)中提供不同的值。要启用此功能,请在选项中传递 `{ injected: true }`。如果在[项目配置](/config/#provide)中未指定该键,则将使用默认值。
259259

260260
:::code-group
261261
```ts [fixtures.test.ts]
262262
import { test as base } from 'vitest'
263263
264264
const test = base.extend({
265265
url: [
266-
// default value if "url" is not defined in the config
266+
// 如果配置中未定义"url",则为默认值
267267
'/default',
268-
// mark the fixture as "injected" to allow the override
268+
// 将夹具标记为"注入"以允许覆盖
269269
{ injected: true },
270270
],
271271
})
272272
273273
test('works correctly', ({ url }) => {
274-
// url is "/default" in "project-new"
275-
// url is "/full" in "project-full"
276-
// url is "/empty" in "project-empty"
274+
// 在"project-new"中,url是"/default"
275+
// 在"project-full"中,url是"/full"
276+
// 在"project-empty"中,url是"/empty"
277277
})
278278
```
279279
```ts [vitest.config.ts]
@@ -309,9 +309,9 @@ export default defineConfig({
309309
```
310310
:::
311311

312-
#### Scoping Values to Suite <Version>3.1.0</Version> {#scoping-values-to-suite}
312+
#### 将值限定到套件范围 <Version>3.1.0</Version> {#scoping-values-to-suite}
313313

314-
Since Vitest 3.1, you can override context values per suite and its children by using the `test.scoped` API:
314+
Vitest 3.1 开始,你可以使用 `test.scoped` API 来按套件及其子项覆盖上下文值:
315315

316316
```ts
317317
import { test as baseTest, describe, expect } from 'vitest'
@@ -325,27 +325,27 @@ describe('use scoped values', () => {
325325
test.scoped({ dependency: 'new' })
326326
327327
test('uses scoped value', ({ dependant }) => {
328-
// `dependant` uses the new overridden value that is scoped
329-
// to all tests in this suite
328+
// `dependant` 使用了新的被覆盖的值,该值是限定范围的
329+
// 到此套件中的所有测试
330330
expect(dependant).toEqual({ dependency: 'new' })
331331
})
332332
333333
describe('keeps using scoped value', () => {
334334
test('uses scoped value', ({ dependant }) => {
335-
// nested suite inherited the value
335+
// 嵌套套件继承了该值
336336
expect(dependant).toEqual({ dependency: 'new' })
337337
})
338338
})
339339
})
340340
341341
test('keep using the default values', ({ dependant }) => {
342-
// the `dependency` is using the default
343-
// value outside of the suite with .scoped
342+
// `dependency` 使用的是默认值
343+
// 在使用 `.scoped` 的套件外部的值
344344
expect(dependant).toEqual({ dependency: 'default' })
345345
})
346346
```
347347

348-
This API is particularly useful if you have a context value that relies on a dynamic variable like a database connection:
348+
如果你有一个依赖于动态变量(如数据库连接)的上下文值,这个 API 特别有用:
349349

350350
```ts
351351
const test = baseTest.extend<{
@@ -373,9 +373,9 @@ describe('another type of schema', () => {
373373
})
374374
```
375375

376-
#### Per-Scope Context <Version>3.2.0</Version>
376+
#### 作用域上下文 <Version>3.2.0</Version>
377377

378-
You can define context that will be initiated once per file or a worker. It is initiated the same way as a regular fixture with an objects parameter:
378+
你可以定义每个文件或每个工作线程只初始化一次的上下文。它的初始化方式与带对象参数的常规夹具相同:
379379

380380
```ts
381381
import { test as baseTest } from 'vitest'
@@ -392,26 +392,26 @@ export const test = baseTest.extend({
392392
})
393393
```
394394

395-
The value is initialised the first time any test has accessed it, unless the fixture options have `auto: true` - in this case the value is initialised before any test has run.
395+
该值在任何测试第一次访问它时初始化,除非夹具选项设置了 `auto: true` - 在这种情况下,该值在任何测试运行之前就已初始化。
396396

397397
```ts
398398
const test = baseTest.extend({
399399
perFile: [
400400
({}, use) => use([]),
401401
{
402402
scope: 'file',
403-
// always run this hook before any test
403+
// 在任何测试之前总是运行这个钩子
404404
auto: true
405405
},
406406
],
407407
})
408408
```
409409

410-
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.
410+
`worker` 作用域将为每个工作线程运行一次夹具。运行的工作线程数量取决于各种因素。默认情况下,每个文件在单独的工作线程中运行,因此 `file` `worker` 作用域的工作方式相同。
411411

412-
However, if you disable [isolation](/config/#isolate), then the number of workers is limited by the [`maxWorkers`](/config/#maxworkers) or [`poolOptions`](/config/#pooloptions) configuration.
412+
但是,如果你禁用了 [isolation](/config/#isolate),那么工作线程的数量将受到 [`maxWorkers`](/config/#maxworkers) [`poolOptions`](/config/#pooloptions) 配置的限制。
413413

414-
Note that specifying `scope: 'worker'` when running tests in `vmThreads` or `vmForks` will work the same way as `scope: 'file'`. This limitation exists because every test file has its own VM context, so if Vitest were to initiate it once, one context could leak to another and create many reference inconsistencies (instances of the same class would reference different constructors, for example).
414+
请注意,在 `vmThreads` `vmForks` 中运行测试时,指定 `scope: 'worker'` 的工作方式与 `scope: 'file'` 相同。这个限制存在是因为每个测试文件都有自己的 VM 上下文,所以如果 Vitest 只初始化一次,一个上下文可能会泄漏到另一个上下文中,并创建许多引用不一致的问题(例如,同一个类的实例会引用不同的构造函数)。
415415

416416
#### TypeScript
417417

@@ -458,7 +458,7 @@ test('types are correct', ({
458458

459459
:::
460460

461-
When using `test.extend`, the extended `test` object provides type-safe `beforeEach` and `afterEach` hooks that are aware of the new context:
461+
当使用 `test.extend` 时,扩展的 `test` 对象提供了类型安全的 `beforeEach` `afterEach` 钩子,这些钩子能够识别新的上下文:
462462

463463
```ts
464464
const test = baseTest.extend<{
@@ -469,7 +469,7 @@ const test = baseTest.extend<{
469469
},
470470
})
471471
472-
// Unlike global hooks, these hooks are aware of the extended context
472+
// 与全局钩子不同,这些钩子能够识别扩展的上下文
473473
test.beforeEach(({ todos }) => {
474474
todos.push(1)
475475
})

0 commit comments

Comments
 (0)