Skip to content

Commit 9f48b75

Browse files
authored
Merge pull request #919 from vitest-dev/sync-6777bfb4-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 6777bfb
2 parents 09900e8 + fb4588c commit 9f48b75

File tree

4 files changed

+102
-20
lines changed

4 files changed

+102
-20
lines changed

api/expect.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ test('the number of elements must match exactly', () => {
782782

783783
## toThrowError
784784

785-
- **类型:** `(received: any) => Awaitable<void>`
785+
- **类型:** `(recexpectedeived: any) => Awaitable<void>`
786786

787787
- **别名:** `toThrow`
788788

@@ -792,7 +792,9 @@ test('the number of elements must match exactly', () => {
792792

793793
- `RegExp`: 错误消息匹配该模式
794794
- `string`: 错误消息包含该子字符串
795-
- `Error`, `AsymmetricMatcher`: 与接收到的对象进行比较,类似于 `toEqual(received)`
795+
- any other value: compare with thrown value using deep equality (similar to `toEqual`)
796+
797+
<!-- TODO: translation -->
796798

797799
:::tip
798800
必须将代码包装在一个函数中,否则错误将无法被捕获,测试将失败。
@@ -853,6 +855,19 @@ test('throws on pineapples', async () => {
853855

854856
:::
855857

858+
<!-- TODO: translation -->
859+
860+
:::tip
861+
You can also test non-Error values that are thrown:
862+
863+
```ts
864+
test('throws non-Error values', () => {
865+
expect(() => { throw 42 }).toThrowError(42)
866+
expect(() => { throw { message: 'error' } }).toThrowError({ message: 'error' })
867+
})
868+
```
869+
:::
870+
856871
## toMatchSnapshot
857872

858873
- **类型:** `<T>(shape?: Partial<T> | string, hint?: string) => void`

api/hooks.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Test hooks are called in a stack order ("after" hooks are reversed) by default,
1313

1414
```ts
1515
function beforeEach(
16-
body: () => unknown,
16+
body: (context: TestContext) => unknown,
1717
timeout?: number,
1818
): void
1919
```
@@ -55,7 +55,7 @@ beforeEach(async () => {
5555

5656
```ts
5757
function afterEach(
58-
body: () => unknown,
58+
body: (context: TestContext) => unknown,
5959
timeout?: number,
6060
): void
6161
```
@@ -83,7 +83,7 @@ You can also use [`onTestFinished`](#ontestfinished) during the test execution t
8383

8484
```ts
8585
function beforeAll(
86-
body: () => unknown,
86+
body: (context: ModuleContext) => unknown,
8787
timeout?: number,
8888
): void
8989
```
@@ -123,7 +123,7 @@ beforeAll(async () => {
123123

124124
```ts
125125
function afterAll(
126-
body: () => unknown,
126+
body: (context: ModuleContext) => unknown,
127127
timeout?: number,
128128
): void
129129
```
@@ -147,7 +147,10 @@ Here the `afterAll` ensures that `stopMocking` method is called after all tests
147147

148148
```ts
149149
function aroundEach(
150-
body: (runTest: () => Promise<void>, context: TestContext) => Promise<void>,
150+
body: (
151+
runTest: () => Promise<void>,
152+
context: TestContext,
153+
) => Promise<void>,
151154
timeout?: number,
152155
): void
153156
```
@@ -254,7 +257,10 @@ test('insert user', async ({ db, user }) => {
254257

255258
```ts
256259
function aroundAll(
257-
body: (runSuite: () => Promise<void>) => Promise<void>,
260+
body: (
261+
runSuite: () => Promise<void>,
262+
context: ModuleContext,
263+
) => Promise<void>,
258264
timeout?: number,
259265
): void
260266
```

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default antfu(
6969
'no-self-compare': 'off',
7070
'import/no-mutable-exports': 'off',
7171
'no-restricted-globals': 'off',
72+
'no-throw-literal': 'off',
7273
},
7374
},
7475
)

guide/test-context.md

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,6 @@ const test = baseTest
280280
.extend('simple', () => 'value')
281281
```
282282

283-
Non-function values only support the `injected` option:
284-
285-
```ts
286-
const test = baseTest
287-
.extend('baseUrl', { injected: true }, 'http://localhost:3000')
288-
.extend('defaults', { port: 3000, host: 'localhost' })
289-
```
290-
291283
#### Accessing Other Fixtures
292284

293285
Each fixture can access previously defined fixtures via its first parameter. This works for both function and non-function fixtures:
@@ -507,15 +499,13 @@ Note that you cannot override non-test fixtures inside `describe` blocks:
507499

508500
```ts
509501
test.describe('a nested suite', () => {
510-
test.override('port', 3000) // throws an error
502+
test.override('port', { scope: 'worker' }, 3000) // throws an error
511503
})
512504
```
513505

514506
Consider overriding it on the top level of the module, or by using [`injected`](#default-fixture-injected) option and providing the value in the project config.
515507

516508
Also note that in [non-isolate](/config/isolate) mode overriding a `worker` fixture will affect the fixture value in all test files running after it was overriden.
517-
518-
<!-- TODO(v5) should this be addressed? force a new worker if worker fixture is overriden? -->
519509
:::
520510

521511
#### Test Scope (Default)
@@ -833,7 +823,7 @@ Note that you cannot introduce new fixtures inside `test.override`. Extend the t
833823

834824
### Type-Safe Hooks
835825

836-
When using `test.extend`, the extended `test` object provides type-safe `beforeEach` and `afterEach` hooks that are aware of the new context:
826+
When using `test.extend`, the extended `test` object provides type-safe hooks that are aware of the extended context:
837827

838828
```ts
839829
const test = baseTest
@@ -848,3 +838,73 @@ test.afterEach(({ counter }) => {
848838
console.log('Final count:', counter.value)
849839
})
850840
```
841+
842+
#### Suite-Level Hooks with Fixtures <Version>4.1.0</Version> {#suite-level-hooks}
843+
844+
The extended `test` object also provides [`beforeAll`](/api/hooks#beforeall), [`afterAll`](/api/hooks#afterall), and [`aroundAll`](/api/hooks#aroundall) hooks that can access file-scoped and worker-scoped fixtures:
845+
846+
```ts
847+
const test = baseTest
848+
.extend('config', { scope: 'file' }, () => loadConfig())
849+
.extend('database', { scope: 'file' }, async ({ config }, { onCleanup }) => {
850+
const db = await createDatabase(config)
851+
onCleanup(() => db.close())
852+
return db
853+
})
854+
855+
// Access file-scoped fixtures in suite-level hooks
856+
test.aroundAll(async (runSuite, { database }) => {
857+
await database.transaction(runSuite)
858+
})
859+
860+
test.beforeAll(async ({ database }) => {
861+
await database.createUsers()
862+
})
863+
864+
test.afterAll(async ({ database }) => {
865+
await database.removeUsers()
866+
})
867+
```
868+
869+
::: warning IMPORTANT
870+
Suite-level hooks (`beforeAll`, `afterAll`, `aroundAll`) **must be called on the `test` object returned from `test.extend()`** to have access to the extended fixtures. Using the global `beforeAll`/`afterAll`/`aroundAll` functions will not have access to your custom fixtures:
871+
872+
```ts
873+
import { test as baseTest, beforeAll } from 'vitest'
874+
875+
const test = baseTest
876+
.extend('database', { scope: 'file' }, async ({}, { onCleanup }) => {
877+
const db = await createDatabase()
878+
onCleanup(() => db.close())
879+
return db
880+
})
881+
882+
// ❌ WRONG: Global beforeAll doesn't have access to 'database'
883+
beforeAll(({ database }) => {
884+
// Error: 'database' is undefined
885+
})
886+
887+
// ✅ CORRECT: Use test.beforeAll to access fixtures
888+
test.beforeAll(({ database }) => {
889+
// 'database' is available
890+
})
891+
```
892+
893+
This applies to all suite-level hooks: `beforeAll`, `afterAll`, and `aroundAll`.
894+
:::
895+
896+
::: tip
897+
Suite-level hooks can only access [**file-scoped** and **worker-scoped** fixtures](#fixture-scopes). Test-scoped fixtures are not available in these hooks because they run outside the context of individual tests. If you try to access a test-scoped fixture in a suite-level hook, Vitest will throw an error.
898+
899+
```ts
900+
const test = baseTest
901+
.extend('testFixture', () => 'test-scoped')
902+
.extend('fileFixture', { scope: 'file' }, () => 'file-scoped')
903+
904+
// ❌ Error: test-scoped fixtures not available in beforeAll
905+
test.beforeAll(({ testFixture }) => {})
906+
907+
// ✅ Works: file-scoped fixtures are available
908+
test.beforeAll(({ fileFixture }) => {})
909+
```
910+
:::

0 commit comments

Comments
 (0)