|
1 |
| -import { render } from '@testing-library/svelte' |
2 |
| -import { describe, expect, test } from 'vitest' |
| 1 | +import { |
| 2 | + afterAll, |
| 3 | + beforeAll, |
| 4 | + beforeEach, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + test, |
| 8 | + vi, |
| 9 | +} from 'vitest' |
3 | 10 |
|
4 | 11 | import Comp from './fixtures/Comp.svelte'
|
| 12 | +import { IS_SVELTE_5 } from './utils.js' |
| 13 | + |
| 14 | +const importSvelteTestingLibrary = async () => |
| 15 | + IS_SVELTE_5 ? import('../svelte5-index.js') : import('../index.js') |
| 16 | + |
| 17 | +const importSvelteTestingLibraryPure = async () => |
| 18 | + IS_SVELTE_5 ? import('../svelte5.js') : import('../pure.js') |
5 | 19 |
|
6 | 20 | 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' } }) |
| 21 | + const afterEach = vi.fn() |
| 22 | + |
| 23 | + beforeAll(() => { |
| 24 | + globalThis.afterEach = afterEach |
12 | 25 | })
|
13 | 26 |
|
14 |
| - test('second', () => { |
15 |
| - expect(document.body.innerHTML).toEqual('') |
| 27 | + afterAll(() => { |
| 28 | + delete globalThis.afterEach |
| 29 | + delete process.env.STL_SKIP_AUTO_CLEANUP |
16 | 30 | })
|
17 |
| -}) |
18 | 31 |
|
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', () => { |
| 32 | + beforeEach(() => { |
| 33 | + vi.resetModules() |
| 34 | + }) |
| 35 | + |
| 36 | + test('calls afterEach with cleanup if globally defined', async () => { |
| 37 | + const { render } = await importSvelteTestingLibrary() |
24 | 38 | render(Comp, { props: { name: 'world' } })
|
25 |
| - render(Comp, { props: { name: 'universe' } }) |
| 39 | + |
| 40 | + expect(afterEach).toHaveBeenCalledTimes(1) |
| 41 | + expect(afterEach).toHaveBeenLastCalledWith(expect.any(Function)) |
| 42 | + |
| 43 | + await afterEach.mock.lastCall[0]() |
| 44 | + |
| 45 | + expect(document.body).toBeEmptyDOMElement() |
| 46 | + }) |
| 47 | + |
| 48 | + test('does not call afterEach if process STL_SKIP_AUTO_CLEANUP is set', async () => { |
| 49 | + process.env.STL_SKIP_AUTO_CLEANUP = 'true' |
| 50 | + |
| 51 | + const { render } = await importSvelteTestingLibrary() |
| 52 | + render(Comp, { props: { name: 'world' } }) |
| 53 | + |
| 54 | + expect(afterEach).toHaveBeenCalledTimes(0) |
26 | 55 | })
|
27 | 56 |
|
28 |
| - test('second', () => { |
29 |
| - expect(document.body.innerHTML).toEqual('') |
| 57 | + test('does not call afterEach if you import from `pure`', async () => { |
| 58 | + const { render } = await importSvelteTestingLibraryPure() |
| 59 | + render(Comp, { props: { name: 'world' } }) |
| 60 | + |
| 61 | + expect(afterEach).toHaveBeenCalledTimes(0) |
30 | 62 | })
|
31 | 63 | })
|
0 commit comments