Skip to content

Commit c1e7d22

Browse files
authored
Add Vitest cleanup hint to react-testing-library setup docs (#1422)
* Add Vitest cleanup hint to react-testing-library setup docs * Document Vitest auto-cleanup for React without enabling globals
1 parent dfd14dc commit c1e7d22

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/react-testing-library/setup.mdx

+38
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,41 @@ And register it using mocha's `-r` flag:
510510
```
511511
mocha --require ./mocha-watch-cleanup-after-each.js
512512
```
513+
514+
### Auto Cleanup in Vitest
515+
516+
If you're using Vitest and want automatic cleanup to work, you can
517+
[enable globals](https://vitest.dev/config/#globals) through its configuration
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)