|
1 |
| -import { render } from '@testing-library/svelte' |
2 |
| -import { describe, expect, test } from 'vitest' |
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
3 | 2 |
|
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() |
5 | 9 |
|
6 | 10 | 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 |
12 | 14 | })
|
13 | 15 |
|
14 |
| - test('second', () => { |
15 |
| - expect(document.body.innerHTML).toEqual('') |
| 16 | + afterEach(() => { |
| 17 | + delete process.env.STL_SKIP_AUTO_CLEANUP |
| 18 | + delete globalThis.afterEach |
16 | 19 | })
|
17 |
| -}) |
18 | 20 |
|
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') |
24 | 29 | render(Comp, { props: { name: 'world' } })
|
25 |
| - render(Comp, { props: { name: 'universe' } }) |
| 30 | + await globalCleanup() |
| 31 | + |
| 32 | + expect(document.body).toBeEmptyDOMElement() |
26 | 33 | })
|
27 | 34 |
|
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) |
30 | 41 | })
|
31 | 42 | })
|
0 commit comments