diff --git a/package.json b/package.json index 5e00bbe..bad9554 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap b/src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap deleted file mode 100644 index 2631c86..0000000 --- a/src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap +++ /dev/null @@ -1,3 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`auto-cleanup-skip > second 1`] = `""`; diff --git a/src/__tests__/auto-cleanup-skip.test.js b/src/__tests__/auto-cleanup-skip.test.js deleted file mode 100644 index db65447..0000000 --- a/src/__tests__/auto-cleanup-skip.test.js +++ /dev/null @@ -1,23 +0,0 @@ -import { beforeAll, describe, expect, test } from 'vitest' - -import Comp from './fixtures/Comp.svelte' - -describe('auto-cleanup-skip', () => { - let render - - beforeAll(async () => { - process.env.STL_SKIP_AUTO_CLEANUP = 'true' - const stl = await import('@testing-library/svelte') - render = stl.render - }) - - // This one verifies that if STL_SKIP_AUTO_CLEANUP is set - // then we DON'T auto-wire up the afterEach for folks - test('first', () => { - render(Comp, { props: { name: 'world' } }) - }) - - test('second', () => { - expect(document.body.innerHTML).toMatchSnapshot() - }) -}) diff --git a/src/__tests__/auto-cleanup.test.js b/src/__tests__/auto-cleanup.test.js index 206d101..b06d120 100644 --- a/src/__tests__/auto-cleanup.test.js +++ b/src/__tests__/auto-cleanup.test.js @@ -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) }) }) diff --git a/src/index.js b/src/index.js index 46fd662..2e3d772 100644 --- a/src/index.js +++ b/src/index.js @@ -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()