Skip to content

Commit 7406070

Browse files
committed
fix: ensure pure module is exported
1 parent ed541de commit 7406070

7 files changed

+58
-49
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"types": "./types/index.d.ts",
99
"default": "./src/index.js"
1010
},
11+
"./pure": {
12+
"types": "./types/index.d.ts",
13+
"default": "./src/pure.js"
14+
},
1115
"./svelte5": {
1216
"types": "./types/index.d.ts",
1317
"default": "./src/svelte5-index.js"
@@ -65,7 +69,6 @@
6569
"setup": "npm install && npm run validate",
6670
"test": "vitest run --coverage",
6771
"test:watch": "vitest",
68-
"test:update": "vitest run --update",
6972
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
7073
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
7174
"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

+50-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
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'
310

411
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')
519

620
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
1225
})
1326

14-
test('second', () => {
15-
expect(document.body.innerHTML).toEqual('')
27+
afterAll(() => {
28+
delete globalThis.afterEach
29+
delete process.env.STL_SKIP_AUTO_CLEANUP
1630
})
17-
})
1831

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()
2438
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)
2655
})
2756

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)
3062
})
3163
})

src/__tests__/cleanup.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('cleanup', () => {
1919
renderSubject()
2020
cleanup()
2121

22-
expect(onDestroyed).toHaveBeenCalledOnce()
22+
expect(onDestroyed).toHaveBeenCalledTimes(1)
2323
})
2424

2525
test('cleanup handles unexpected errors during mount', () => {

src/__tests__/mount.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('mount and destroy', () => {
1515

1616
expect(content).toBeInTheDocument()
1717
await act()
18-
expect(onMounted).toHaveBeenCalledOnce()
18+
expect(onMounted).toHaveBeenCalledTimes(1)
1919
})
2020

2121
test('component is destroyed', async () => {
@@ -28,6 +28,6 @@ describe('mount and destroy', () => {
2828

2929
expect(content).not.toBeInTheDocument()
3030
await act()
31-
expect(onDestroyed).toHaveBeenCalledOnce()
31+
expect(onDestroyed).toHaveBeenCalledTimes(1)
3232
})
3333
})

src/__tests__/rerender.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('rerender', () => {
2323
await rerender({ props: { name: 'Dolly' } })
2424

2525
expect(element).toHaveTextContent('Hello Dolly!')
26-
expect(console.warn).toHaveBeenCalledOnce()
26+
expect(console.warn).toHaveBeenCalledTimes(1)
2727
expect(console.warn).toHaveBeenCalledWith(
2828
expect.stringMatching(/deprecated/iu)
2929
)

0 commit comments

Comments
 (0)