From 88fb8cd38cc260fbb88f2d42e4a66a500b56f44a Mon Sep 17 00:00:00 2001 From: Maxim Mazurok Date: Tue, 31 Oct 2023 19:34:23 +1100 Subject: [PATCH] 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. --- src/__tests__/auto-cleanup-vitest-globals.js | 7 +++++++ src/__tests__/auto-cleanup-vitest.js | 10 ++++++++++ src/index.js | 5 +++++ 3 files changed, 22 insertions(+) create mode 100644 src/__tests__/auto-cleanup-vitest-globals.js create mode 100644 src/__tests__/auto-cleanup-vitest.js diff --git a/src/__tests__/auto-cleanup-vitest-globals.js b/src/__tests__/auto-cleanup-vitest-globals.js new file mode 100644 index 0000000..8d320ea --- /dev/null +++ b/src/__tests__/auto-cleanup-vitest-globals.js @@ -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() +}) diff --git a/src/__tests__/auto-cleanup-vitest.js b/src/__tests__/auto-cleanup-vitest.js new file mode 100644 index 0000000..ef0a5a1 --- /dev/null +++ b/src/__tests__/auto-cleanup-vitest.js @@ -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' + `) +}) diff --git a/src/index.js b/src/index.js index b9f4dc4..99a01b7 100644 --- a/src/index.js +++ b/src/index.js @@ -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'