Skip to content

test: fix auto-cleanup tests #371

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

Merged
merged 6 commits into from
May 15, 2024
Merged
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"setup": "npm install && npm run validate",
"test": "vitest run --coverage",
"test:watch": "vitest",
"test:update": "vitest run --update",
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
"types": "svelte-check",
Expand Down
3 changes: 0 additions & 3 deletions src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap

This file was deleted.

23 changes: 0 additions & 23 deletions src/__tests__/auto-cleanup-skip.test.js

This file was deleted.

49 changes: 30 additions & 19 deletions src/__tests__/auto-cleanup.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import { render } from '@testing-library/svelte'
import { describe, expect, test } from 'vitest'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'

import Comp from './fixtures/Comp.svelte'
import { IS_SVELTE_5 } from './utils.js'

const importSvelteTestingLibrary = async () =>
IS_SVELTE_5 ? import('../svelte5-index.js') : import('../index.js')

const globalAfterEach = vi.fn()

describe('auto-cleanup', () => {
// This just verifies that by importing STL in an
// environment which supports afterEach (like jest)
// we'll get automatic cleanup between tests.
test('first', () => {
render(Comp, { props: { name: 'world' } })
beforeEach(() => {
vi.resetModules()
globalThis.afterEach = globalAfterEach
})

test('second', () => {
expect(document.body.innerHTML).toEqual('')
afterEach(() => {
delete process.env.STL_SKIP_AUTO_CLEANUP
delete globalThis.afterEach
})
})

describe('cleanup of two components', () => {
// This just verifies that by importing STL in an
// environment which supports afterEach (like jest)
// we'll get automatic cleanup between tests.
test('first', () => {
test('calls afterEach with cleanup if globally defined', async () => {
const { render } = await importSvelteTestingLibrary()

expect(globalAfterEach).toHaveBeenCalledTimes(1)
expect(globalAfterEach).toHaveBeenLastCalledWith(expect.any(Function))
const globalCleanup = globalAfterEach.mock.lastCall[0]

const { default: Comp } = await import('./fixtures/Comp.svelte')
render(Comp, { props: { name: 'world' } })
render(Comp, { props: { name: 'universe' } })
await globalCleanup()

expect(document.body).toBeEmptyDOMElement()
})

test('second', () => {
expect(document.body.innerHTML).toEqual('')
test('does not call afterEach if process STL_SKIP_AUTO_CLEANUP is set', async () => {
process.env.STL_SKIP_AUTO_CLEANUP = 'true'

await importSvelteTestingLibrary()

expect(globalAfterEach).toHaveBeenCalledTimes(0)
})
})
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { act, cleanup } from './pure.js'
// If we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
// this ensures that tests run in isolation from each other
// if you don't like this then either import the `pure` module
// or set the STL_SKIP_AUTO_CLEANUP env variable to 'true'.
// if you don't like this then set the STL_SKIP_AUTO_CLEANUP env variable.
if (typeof afterEach === 'function' && !process.env.STL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await act()
Expand Down
Loading