-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
31 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters