Skip to content

Commit 0043700

Browse files
committed
Document Vitest auto-cleanup for React without enabling globals
1 parent 8a9bf16 commit 0043700

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

docs/react-testing-library/setup.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,38 @@ mocha --require ./mocha-watch-cleanup-after-each.js
513513

514514
### Auto Cleanup in Vitest
515515

516-
If you're using Vitest and want automatic cleanup to work, you need to
516+
If you're using Vitest and want automatic cleanup to work, you can
517517
[enable globals](https://vitest.dev/config/#globals) through its configuration
518-
file.
518+
file:
519+
520+
```ts title="vitest.config.ts"
521+
import {defineConfig} from 'vitest/config'
522+
523+
export default defineConfig({
524+
test: {
525+
globals: true,
526+
},
527+
})
528+
```
529+
530+
If you don't want to enable globals, you can import `cleanup` and call it
531+
manually in a top-level `afterEach` hook:
532+
533+
```ts title="vitest.config.ts"
534+
import {defineConfig} from 'vitest/config'
535+
536+
export default defineConfig({
537+
test: {
538+
setupFiles: ['vitest-cleanup-after-each.ts'],
539+
},
540+
})
541+
```
542+
543+
```ts title="vitest-cleanup-after-each.ts"
544+
import {cleanup} from '@testing-library/react'
545+
import {afterEach} from 'vitest'
546+
547+
afterEach(() => {
548+
cleanup()
549+
})
550+
```

0 commit comments

Comments
 (0)