From 0a3b61933f02ecb77375cb691516eddbdeb9dce0 Mon Sep 17 00:00:00 2001 From: freakzlike Date: Sat, 8 Apr 2023 10:04:21 +0200 Subject: [PATCH] fix(renderToString): Remove document usage on SSR render --- src/createInstance.ts | 19 ---------------- src/mount.ts | 22 +++++++++++++++---- tests/__snapshots__/shallowMount.spec.ts.snap | 2 +- tests/renderToString.spec.ts | 5 +++++ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/createInstance.ts b/src/createInstance.ts index 394574963..434556195 100644 --- a/src/createInstance.ts +++ b/src/createInstance.ts @@ -111,24 +111,6 @@ export function createInstance( // Let's register it as a stub so user can find it registerStub({ source: originalComponent, stub: component }) - const el = document.createElement('div') - - if (options?.attachTo) { - let to: Element | null - if (typeof options.attachTo === 'string') { - to = document.querySelector(options.attachTo) - if (!to) { - throw new Error( - `Unable to find the element matching the selector ${options.attachTo} given as the \`attachTo\` option` - ) - } - } else { - to = options.attachTo - } - - to.appendChild(el) - } - function slotToFunction(slot: Slot) { switch (typeof slot) { case 'function': @@ -359,7 +341,6 @@ export function createInstance( return { app, - el, props, componentRef } diff --git a/src/mount.ts b/src/mount.ts index 9133fa4ee..0fe8ce4e8 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -248,10 +248,7 @@ export function mount( inputComponent: any, options?: MountingOptions & Record ): VueWrapper { - const { app, props, el, componentRef } = createInstance( - inputComponent, - options - ) + const { app, props, componentRef } = createInstance(inputComponent, options) const setProps = (newProps: Record) => { for (const [k, v] of Object.entries(newProps)) { @@ -272,6 +269,23 @@ export function mount( } // mount the app! + const el = document.createElement('div') + + if (options?.attachTo) { + let to: Element | null + if (typeof options.attachTo === 'string') { + to = document.querySelector(options.attachTo) + if (!to) { + throw new Error( + `Unable to find the element matching the selector ${options.attachTo} given as the \`attachTo\` option` + ) + } + } else { + to = options.attachTo + } + + to.appendChild(el) + } const vm = app.mount(el) if (errorOnMount) { diff --git a/tests/__snapshots__/shallowMount.spec.ts.snap b/tests/__snapshots__/shallowMount.spec.ts.snap index 8db58ddee..9ade8de88 100644 --- a/tests/__snapshots__/shallowMount.spec.ts.snap +++ b/tests/__snapshots__/shallowMount.spec.ts.snap @@ -1,4 +1,4 @@ -// Vitest Snapshot v1 +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`shallowMount > renders props for stubbed component in a snapshot 1`] = `
diff --git a/tests/renderToString.spec.ts b/tests/renderToString.spec.ts index eb1669fae..02724331b 100644 --- a/tests/renderToString.spec.ts +++ b/tests/renderToString.spec.ts @@ -1,3 +1,8 @@ +/** + * Run SSR tests in node environment + * @vitest-environment node + */ + import { describe, it, expect } from 'vitest' import { defineComponent, onMounted, onServerPrefetch, ref } from 'vue' import { renderToString } from '../src'