Skip to content

Commit

Permalink
Merge branch 'main' into test-types
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/release.yml
#	package.json
  • Loading branch information
mcous committed Feb 1, 2024
2 parents 6187605 + c8158a9 commit 5c9b218
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 82 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ jobs:
main:
# ignore all-contributors PRs
if: ${{ !contains(github.head_ref, 'all-contributors') }}
name: Node ${{ matrix.node }}, Svelte ${{ matrix.svelte }}
name: Node ${{ matrix.node }}, Svelte ${{ matrix.svelte }}, ${{ matrix.test-runner }}
runs-on: ubuntu-latest
strategy:
matrix:
node: ['16', '18', '20']
svelte: ['3', '4']
runs-on: ubuntu-latest
test-runner: ['vitest:jsdom', 'vitest:happy-dom']

steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
Expand All @@ -35,7 +37,7 @@ jobs:
npm install --no-save svelte@${{ matrix.svelte }}
- name: ▶️ Run tests
run: npm test
run: npm run test:${{ matrix.test-runner }}

- name: ▶️ Run type-checks
if: ${{ matrix.node == '20' && matrix.svelte == '4' }}
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
"format:delta": "npm-run-all format:prettier:delta format:eslint:delta",
"format:prettier:delta": "prettier --write `./scripts/changed-files`",
"format:eslint:delta": "eslint --fix `./scripts/changed-files`",
"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",
"setup": "npm install && npm run validate",
"validate": "npm-run-all test types",
"validate": "npm-run-all test:vitest:* types",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate"
},
Expand Down Expand Up @@ -85,7 +87,12 @@
"eslint-plugin-simple-import-sort": "10.0.0",
"eslint-plugin-svelte": "2.35.1",
"eslint-plugin-vitest-globals": "1.4.0",
<<<<<<< HEAD
"expect-type": "^0.17.3",
||||||| c568b5d
=======
"happy-dom": "^13.3.1",
>>>>>>> main
"jsdom": "^22.1.0",
"npm-run-all": "^4.1.5",
"prettier": "3.2.4",
Expand Down
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: {
// 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'],
},
},
})
}))

0 comments on commit 5c9b218

Please sign in to comment.