import consola from 'consola' import { timeout } from '@nuxt/common' export default async function renderAndGetWindow( url = 'http://localhost:3000', jsdomOpts = {}, { loadedCallback, loadingTimeout = 2000, ssr, globals } = {} ) { const jsdom = await import('jsdom') .then(m => m.default || m) .catch((e) => { consola.error(` jsdom is not installed. Please install jsdom with: $ yarn add --dev jsdom OR $ npm install --dev jsdom `) throw e }) const options = Object.assign({ // Load subresources (https://github.com/tmpvar/jsdom#loading-subresources) resources: 'usable', runScripts: 'dangerously', virtualConsole: true, beforeParse(window) { // Mock window.scrollTo window.scrollTo = () => {} } }, jsdomOpts) const jsdomErrHandler = (err) => { throw err } if (options.virtualConsole) { if (options.virtualConsole === true) { options.virtualConsole = new jsdom.VirtualConsole().sendTo(consola) } // Throw error when window creation failed options.virtualConsole.on('jsdomError', jsdomErrHandler) } const { window } = await jsdom.JSDOM.fromURL(url, options) // If Nuxt could not be loaded (error from the server-side) const nuxtExists = window.document.body.innerHTML.includes( ssr ? `window.${globals.context}` : `