From b963beaf4d6f04eb91c4d1810abc94e821fc119e Mon Sep 17 00:00:00 2001 From: Isaiah Thomason <47364027+ITenthusiasm@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:49:36 -0500 Subject: [PATCH 1/3] refactor: Warn Users Lacking Global `afterEach` (without Errors) This approach provides a much friendlier developer experience to those who want to avoid using globals. It's also less brittle. --- src/__tests__/auto-cleanup-vitest-globals.js | 10 +++++----- src/__tests__/auto-cleanup-vitest.js | 19 +++++++++++-------- src/index.js | 8 ++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/__tests__/auto-cleanup-vitest-globals.js b/src/__tests__/auto-cleanup-vitest-globals.js index 8d320ea..3f2fbed 100644 --- a/src/__tests__/auto-cleanup-vitest-globals.js +++ b/src/__tests__/auto-cleanup-vitest-globals.js @@ -1,7 +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' +test('Testing Utilities with global access to `afterEach()` will not log warnings', () => { + jest.spyOn(console, 'warn') + global.afterEach = () => {} - expect(() => require('..')).not.toThrow() + require('..') + expect(console.warn).not.toHaveBeenCalled() }) diff --git a/src/__tests__/auto-cleanup-vitest.js b/src/__tests__/auto-cleanup-vitest.js index ef0a5a1..8540257 100644 --- a/src/__tests__/auto-cleanup-vitest.js +++ b/src/__tests__/auto-cleanup-vitest.js @@ -1,10 +1,13 @@ -// 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' +test('Testing Utilities lacking a global `afterEach` function will log a warning ONCE', () => { + delete global.afterEach + const warn = jest.spyOn(console, 'warn') - 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' - `) + 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/index.js b/src/index.js index 99a01b7..a3d4219 100644 --- a/src/index.js +++ b/src/index.js @@ -8,10 +8,10 @@ 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'", +} 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.`, ) } From 3b006953bd43b6e80310c56de0c1227d77c68d9f Mon Sep 17 00:00:00 2001 From: Isaiah Thomason <47364027+ITenthusiasm@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:55:48 -0500 Subject: [PATCH 2/3] style: Decrease the Lines Used for Auto `cleanup` --- src/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a3d4219..3fb1b0c 100644 --- a/src/index.js +++ b/src/index.js @@ -5,9 +5,7 @@ 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() - }) + afterEach(cleanup) } else if (!process.env.VTL_AFTEREACH_WARNING_LOGGED) { process.env.VTL_AFTEREACH_WARNING_LOGGED = true console.warn( From 4025d2a34ef760c244cb49edfa73647d1ad8f5ed Mon Sep 17 00:00:00 2001 From: Isaiah Thomason <47364027+ITenthusiasm@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:57:14 -0500 Subject: [PATCH 3/3] style: Combine Tests Related to Test Running Env --- ...to-cleanup-vitest.js => auto-cleanup-global-env.js} | 10 ++++++++++ src/__tests__/auto-cleanup-vitest-globals.js | 7 ------- 2 files changed, 10 insertions(+), 7 deletions(-) rename src/__tests__/{auto-cleanup-vitest.js => auto-cleanup-global-env.js} (66%) delete mode 100644 src/__tests__/auto-cleanup-vitest-globals.js diff --git a/src/__tests__/auto-cleanup-vitest.js b/src/__tests__/auto-cleanup-global-env.js similarity index 66% rename from src/__tests__/auto-cleanup-vitest.js rename to src/__tests__/auto-cleanup-global-env.js index 8540257..6c2831d 100644 --- a/src/__tests__/auto-cleanup-vitest.js +++ b/src/__tests__/auto-cleanup-global-env.js @@ -1,3 +1,13 @@ +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') diff --git a/src/__tests__/auto-cleanup-vitest-globals.js b/src/__tests__/auto-cleanup-vitest-globals.js deleted file mode 100644 index 3f2fbed..0000000 --- a/src/__tests__/auto-cleanup-vitest-globals.js +++ /dev/null @@ -1,7 +0,0 @@ -test('Testing Utilities with global access to `afterEach()` will not log warnings', () => { - jest.spyOn(console, 'warn') - global.afterEach = () => {} - - require('..') - expect(console.warn).not.toHaveBeenCalled() -})