Skip to content

Commit ac3248d

Browse files
authored
test: fix auto-cleanup tests (#371)
1 parent 266e2df commit ac3248d

File tree

5 files changed

+31
-48
lines changed

5 files changed

+31
-48
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
"setup": "npm install && npm run validate",
6666
"test": "vitest run --coverage",
6767
"test:watch": "vitest",
68-
"test:update": "vitest run --update",
6968
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
7069
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
7170
"types": "svelte-check",

src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap

-3
This file was deleted.

src/__tests__/auto-cleanup-skip.test.js

-23
This file was deleted.

src/__tests__/auto-cleanup.test.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
1-
import { render } from '@testing-library/svelte'
2-
import { describe, expect, test } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
32

4-
import Comp from './fixtures/Comp.svelte'
3+
import { IS_SVELTE_5 } from './utils.js'
4+
5+
const importSvelteTestingLibrary = async () =>
6+
IS_SVELTE_5 ? import('../svelte5-index.js') : import('../index.js')
7+
8+
const globalAfterEach = vi.fn()
59

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

14-
test('second', () => {
15-
expect(document.body.innerHTML).toEqual('')
16+
afterEach(() => {
17+
delete process.env.STL_SKIP_AUTO_CLEANUP
18+
delete globalThis.afterEach
1619
})
17-
})
1820

19-
describe('cleanup of two components', () => {
20-
// This just verifies that by importing STL in an
21-
// environment which supports afterEach (like jest)
22-
// we'll get automatic cleanup between tests.
23-
test('first', () => {
21+
test('calls afterEach with cleanup if globally defined', async () => {
22+
const { render } = await importSvelteTestingLibrary()
23+
24+
expect(globalAfterEach).toHaveBeenCalledTimes(1)
25+
expect(globalAfterEach).toHaveBeenLastCalledWith(expect.any(Function))
26+
const globalCleanup = globalAfterEach.mock.lastCall[0]
27+
28+
const { default: Comp } = await import('./fixtures/Comp.svelte')
2429
render(Comp, { props: { name: 'world' } })
25-
render(Comp, { props: { name: 'universe' } })
30+
await globalCleanup()
31+
32+
expect(document.body).toBeEmptyDOMElement()
2633
})
2734

28-
test('second', () => {
29-
expect(document.body.innerHTML).toEqual('')
35+
test('does not call afterEach if process STL_SKIP_AUTO_CLEANUP is set', async () => {
36+
process.env.STL_SKIP_AUTO_CLEANUP = 'true'
37+
38+
await importSvelteTestingLibrary()
39+
40+
expect(globalAfterEach).toHaveBeenCalledTimes(0)
3041
})
3142
})

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { act, cleanup } from './pure.js'
44
// If we're running in a test runner that supports afterEach
55
// then we'll automatically run cleanup afterEach test
66
// this ensures that tests run in isolation from each other
7-
// if you don't like this then either import the `pure` module
8-
// or set the STL_SKIP_AUTO_CLEANUP env variable to 'true'.
7+
// if you don't like this then set the STL_SKIP_AUTO_CLEANUP env variable.
98
if (typeof afterEach === 'function' && !process.env.STL_SKIP_AUTO_CLEANUP) {
109
afterEach(async () => {
1110
await act()

0 commit comments

Comments
 (0)