diff --git a/src/__tests__/auto-cleanup-global-env.js b/src/__tests__/auto-cleanup-global-env.js new file mode 100644 index 0000000..6c2831d --- /dev/null +++ b/src/__tests__/auto-cleanup-global-env.js @@ -0,0 +1,23 @@ +beforeEach(jest.restoreAllMocks) + +test('Testing Utilities with global access to `afterEach()` will not log warnings', () => { + jest.spyOn(console, 'warn') + global.afterEach = () => {} + + require('..') + expect(console.warn).not.toHaveBeenCalled() +}) + +test('Testing Utilities lacking a global `afterEach` function will log a warning ONCE', () => { + delete global.afterEach + const warn = jest.spyOn(console, 'warn') + + require('..') + jest.resetModules() + require('..') + + expect(warn).toHaveBeenCalledTimes(1) + expect(warn.mock.calls[0][0]).toMatchInlineSnapshot( + `The current test runner does not support afterEach/teardown hooks. This means we won't be able to run automatic cleanup and you should be calling cleanup() manually.`, + ) +}) diff --git a/src/__tests__/auto-cleanup-vitest-globals.js b/src/__tests__/auto-cleanup-vitest-globals.js deleted file mode 100644 index 8d320ea..0000000 --- a/src/__tests__/auto-cleanup-vitest-globals.js +++ /dev/null @@ -1,7 +0,0 @@ -// 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 deleted file mode 100644 index ef0a5a1..0000000 --- a/src/__tests__/auto-cleanup-vitest.js +++ /dev/null @@ -1,10 +0,0 @@ -// 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 99a01b7..3fb1b0c 100644 --- a/src/index.js +++ b/src/index.js @@ -5,13 +5,11 @@ import {cleanup} from './render' // This ensures that tests run in isolation from each other. // If you don't like this, set the VTL_SKIP_AUTO_CLEANUP variable to 'true'. 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'", + afterEach(cleanup) +} else if (!process.env.VTL_AFTEREACH_WARNING_LOGGED) { + process.env.VTL_AFTEREACH_WARNING_LOGGED = true + console.warn( + `The current test runner does not support afterEach/teardown hooks. This means we won't be able to run automatic cleanup and you should be calling cleanup() manually.`, ) }