Skip to content

Commit 88fb8cd

Browse files
bug: Fix issue on Vite env not cleaning up after test (#297)
BREAKING CHANGE: This PR change default behavior for Vitest users. This major should yield no effect to other test frameworks. Throw error when running vitest with no `afterEach` global and no `VTL_SKIP_AUTO_CLEANUP` flag is set.
1 parent 015de7d commit 88fb8cd

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This test verifies that if test is running from vitest with globals - jest will not throw
2+
test('works', () => {
3+
global.afterEach = () => {} // emulate enabled globals
4+
process.env.VITEST = 'true'
5+
6+
expect(() => require('..')).not.toThrow()
7+
})

src/__tests__/auto-cleanup-vitest.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This test verifies that if test is running from vitest without globals - jest will throw
2+
test('works', () => {
3+
delete global.afterEach // no globals in vitest by default
4+
process.env.VITEST = 'true'
5+
6+
expect(() => require('..')).toThrowErrorMatchingInlineSnapshot(`
7+
You are using vitest without globals, this way we can't run cleanup after each test.
8+
See https://testing-library.com/docs/vue-testing-library/setup for details or set the VTL_SKIP_AUTO_CLEANUP variable to 'true'
9+
`)
10+
})

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ if (typeof afterEach === 'function' && !process.env.VTL_SKIP_AUTO_CLEANUP) {
88
afterEach(() => {
99
cleanup()
1010
})
11+
} else if (process.env.VITEST === 'true') {
12+
throw new Error(
13+
"You are using vitest without globals, this way we can't run cleanup after each test.\n" +
14+
"See https://testing-library.com/docs/vue-testing-library/setup for details or set the VTL_SKIP_AUTO_CLEANUP variable to 'true'",
15+
)
1116
}
1217

1318
export * from '@testing-library/dom'

0 commit comments

Comments
 (0)