Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add onMount test and replace onDestory test #303

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/Mounter.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { onDestroy,onMount } from 'svelte'

export let onMounted
export let onDestroyed

onMount(() => onMounted())
onDestroy(() => onDestroyed())
</script>

<button />
40 changes: 0 additions & 40 deletions src/__tests__/fixtures/Stopwatch.svelte

This file was deleted.

33 changes: 33 additions & 0 deletions src/__tests__/mount.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, test, vi } from 'vitest'

import { act, render, screen } from '..'
import Mounter from './fixtures/Mounter.svelte'

describe('mount and destroy', () => {
const handleMount = vi.fn()
const handleDestroy = vi.fn()

test('component is mounted', async () => {
await act(() => {
render(Mounter, { onMounted: handleMount, onDestroyed: handleDestroy })
})

const content = screen.getByRole('button')

expect(handleMount).toHaveBeenCalledOnce()
expect(content).toBeInTheDocument()
})

test('component is destroyed', async () => {
const { unmount } = render(Mounter, {
onMounted: handleMount,
onDestroyed: handleDestroy,
})

await act(() => unmount())
const content = screen.queryByRole('button')

expect(handleDestroy).toHaveBeenCalledOnce()
expect(content).not.toBeInTheDocument()
})
})
35 changes: 0 additions & 35 deletions src/__tests__/unmount.test.js

This file was deleted.

13 changes: 11 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
export default defineConfig(({ mode }) => ({
plugins: [svelte()],
resolve: {
mcous marked this conversation as resolved.
Show resolved Hide resolved
// Ensure `browser` exports are used in tests
// Vitest prefers modules' `node` export by default
// Svelte's `node` export is its SSR bundle, which does not have onMount
// https://github.com/testing-library/svelte-testing-library/issues/222#issuecomment-1909993331
conditions: mode === 'test' ? ['browser'] : [],
},
test: {
environment: 'jsdom',
setupFiles: ['./src/__tests__/_vitest-setup.js'],
mockReset: true,
unstubGlobals: true,
coverage: {
provider: 'v8',
include: ['src'],
},
},
})
}))
Loading