Skip to content

Commit

Permalink
bug: Fix issue on Vite env not cleaning up after test (#297)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Maxim-Mazurok authored Oct 31, 2023
1 parent 015de7d commit 88fb8cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/auto-cleanup-vitest-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This test verifies that if test is running from vitest with globals - jest will not throw
test('works', () => {
global.afterEach = () => {} // emulate enabled globals
process.env.VITEST = 'true'

expect(() => require('..')).not.toThrow()
})
10 changes: 10 additions & 0 deletions src/__tests__/auto-cleanup-vitest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This test verifies that if test is running from vitest without globals - jest will throw
test('works', () => {
delete global.afterEach // no globals in vitest by default
process.env.VITEST = 'true'

expect(() => require('..')).toThrowErrorMatchingInlineSnapshot(`
You are using vitest without globals, this way we can't run cleanup after each test.
See https://testing-library.com/docs/vue-testing-library/setup for details or set the VTL_SKIP_AUTO_CLEANUP variable to 'true'
`)
})
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if (typeof afterEach === 'function' && !process.env.VTL_SKIP_AUTO_CLEANUP) {
afterEach(() => {
cleanup()
})
} else if (process.env.VITEST === 'true') {
throw new Error(
"You are using vitest without globals, this way we can't run cleanup after each test.\n" +
"See https://testing-library.com/docs/vue-testing-library/setup for details or set the VTL_SKIP_AUTO_CLEANUP variable to 'true'",
)
}

export * from '@testing-library/dom'
Expand Down

0 comments on commit 88fb8cd

Please sign in to comment.