Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn Developers Lacking a Global afterEach Function Instead of Throwing Errors #309

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/__tests__/auto-cleanup-global-env.js
Original file line number Diff line number Diff line change
@@ -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.`,
)
})
7 changes: 0 additions & 7 deletions src/__tests__/auto-cleanup-vitest-globals.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/__tests__/auto-cleanup-vitest.js

This file was deleted.

12 changes: 5 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
)
}

Expand Down
Loading