From 178b2dee9c9f3719e88e5e513a562e541532c1e5 Mon Sep 17 00:00:00 2001 From: Michael Cousins Date: Tue, 13 Feb 2024 13:11:23 -0500 Subject: [PATCH] feat: add Svelte v5-next support (#321) * fix: remove DOM elements even if component creation fails Fixes #190 * feat: add Svelte v5-next support --- .github/workflows/release.yml | 12 +++ package.json | 8 +- .../__snapshots__/render.test.js.snap | 27 ++++++- src/__tests__/cleanup.test.js | 35 +++++++++ src/__tests__/fixtures/Mounter.svelte | 18 +++-- src/__tests__/fixtures/Rerender.svelte | 20 ++--- src/__tests__/mount.test.js | 26 +++---- src/__tests__/render.test.js | 61 +++++++++------ src/__tests__/rerender.test.js | 19 +++-- src/__tests__/transition.test.js | 3 +- src/pure.js | 74 +++++++++++-------- vite.config.js | 2 +- 12 files changed, 210 insertions(+), 95 deletions(-) create mode 100644 src/__tests__/cleanup.test.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c6e83fa..ab433cc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,11 +16,23 @@ jobs: if: ${{ !contains(github.head_ref, 'all-contributors') }} name: Node ${{ matrix.node }}, Svelte ${{ matrix.svelte }}, ${{ matrix.test-runner }} runs-on: ubuntu-latest + continue-on-error: ${{ matrix.experimental }} strategy: + fail-fast: false matrix: node: ['16', '18', '20'] svelte: ['3', '4'] test-runner: ['vitest:jsdom', 'vitest:happy-dom'] + experimental: [false] + include: + - node: '20' + svelte: 'next' + test-runner: 'vitest:jsdom' + experimental: true + - node: '20' + svelte: 'next' + test-runner: 'vitest:happy-dom' + experimental: true steps: - name: ⬇️ Checkout repo diff --git a/package.json b/package.json index 20bc1c2..938f563 100644 --- a/package.json +++ b/package.json @@ -64,13 +64,13 @@ "contributors:generate": "all-contributors generate" }, "peerDependencies": { - "svelte": "^3 || ^4" + "svelte": "^3 || ^4 || ^5" }, "dependencies": { "@testing-library/dom": "^9.3.1" }, "devDependencies": { - "@sveltejs/vite-plugin-svelte": "^2.4.2", + "@sveltejs/vite-plugin-svelte": "^3.0.2", "@testing-library/jest-dom": "^6.3.0", "@testing-library/user-event": "^14.5.2", "@typescript-eslint/eslint-plugin": "6.19.1", @@ -94,11 +94,11 @@ "npm-run-all": "^4.1.5", "prettier": "3.2.4", "prettier-plugin-svelte": "3.1.2", - "svelte": "^3 || ^4", + "svelte": "^4.2.10", "svelte-check": "^3.6.3", "svelte-jester": "^3.0.0", "typescript": "^5.3.3", - "vite": "^4.3.9", + "vite": "^5.1.1", "vitest": "^0.33.0" } } diff --git a/src/__tests__/__snapshots__/render.test.js.snap b/src/__tests__/__snapshots__/render.test.js.snap index a31cf2e..b9eb849 100644 --- a/src/__tests__/__snapshots__/render.test.js.snap +++ b/src/__tests__/__snapshots__/render.test.js.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`render > should accept svelte component options 1`] = ` +exports[`render > should accept svelte v4 component options 1`] = `

should accept svelte component options 1`] = ` -
`; + +exports[`render > should accept svelte v5 component options 1`] = ` + + + + +
+

+ Hello World! +

+ +
+ we have context +
+ + + +
+ +`; diff --git a/src/__tests__/cleanup.test.js b/src/__tests__/cleanup.test.js new file mode 100644 index 0000000..789338d --- /dev/null +++ b/src/__tests__/cleanup.test.js @@ -0,0 +1,35 @@ +import { describe, expect, test, vi } from 'vitest' + +import { act, cleanup, render } from '..' +import Mounter from './fixtures/Mounter.svelte' + +const onExecuted = vi.fn() +const onDestroyed = vi.fn() +const renderSubject = () => render(Mounter, { onExecuted, onDestroyed }) + +describe('cleanup', () => { + test('cleanup deletes element', async () => { + renderSubject() + cleanup() + + expect(document.body).toBeEmptyDOMElement() + }) + + test('cleanup unmounts component', async () => { + await act(renderSubject) + cleanup() + + expect(onDestroyed).toHaveBeenCalledOnce() + }) + + test('cleanup handles unexpected errors during mount', () => { + onExecuted.mockImplementation(() => { + throw new Error('oh no!') + }) + + expect(renderSubject).toThrowError() + cleanup() + + expect(document.body).toBeEmptyDOMElement() + }) +}) diff --git a/src/__tests__/fixtures/Mounter.svelte b/src/__tests__/fixtures/Mounter.svelte index 477bb34..51ebcd8 100644 --- a/src/__tests__/fixtures/Mounter.svelte +++ b/src/__tests__/fixtures/Mounter.svelte @@ -1,11 +1,19 @@