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'