From 488010bf78854d8017a79e28ac4cc644e5ac98cc Mon Sep 17 00:00:00 2001 From: Sebastien Chopin Date: Tue, 30 May 2017 16:08:51 +0200 Subject: [PATCH] Upgrade dependencies and JSDOM API --- examples/with-ava/package.json | 8 ++-- examples/with-ava/test/index.test.js | 2 +- lib/app/client.js | 4 ++ lib/render.js | 55 ++++++++++++---------------- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/examples/with-ava/package.json b/examples/with-ava/package.json index e05b6c01f1..f8925f9997 100755 --- a/examples/with-ava/package.json +++ b/examples/with-ava/package.json @@ -4,13 +4,13 @@ "dev": "nuxt", "build": "nuxt build", "start": "nuxt start", - "test": "ava" + "test": "ava --serial --verbose" }, "devDependencies": { - "ava": "^0.16.0", - "jsdom": "^9.8.3" + "ava": "^0.19.1", + "jsdom": "^11.0.0" }, "dependencies": { - "nuxt": "^0.9.5" + "nuxt": "^1.0.0-alpha2" } } diff --git a/examples/with-ava/test/index.test.js b/examples/with-ava/test/index.test.js index 21c25e9320..e5249406f1 100755 --- a/examples/with-ava/test/index.test.js +++ b/examples/with-ava/test/index.test.js @@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => { try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {} config.rootDir = rootDir // project folder config.dev = false // production build - nuxt = new Nuxt(config) + nuxt = await new Nuxt(config) await nuxt.build() server = new nuxt.Server(nuxt) server.listen(4000, 'localhost') diff --git a/lib/app/client.js b/lib/app/client.js index 0b47a07cdf..5b92f79b8c 100644 --- a/lib/app/client.js +++ b/lib/app/client.js @@ -349,6 +349,10 @@ function nuxtReady (app) { cb(app) } }) + // Special JSDOM + if (typeof window._onNuxtLoaded === 'function') { + window._onNuxtLoaded(app) + } // Add router hooks router.afterEach(function (to, from) { app.$nuxt.$emit('routeChanged', to, from) diff --git a/lib/render.js b/lib/render.js index 2365af27d4..92b4cf789e 100644 --- a/lib/render.js +++ b/lib/render.js @@ -138,12 +138,11 @@ export async function renderRoute (url, context = {}) { // Function used to do dom checking via jsdom let jsdom = null -export function renderAndGetWindow (url, opts = {}) { +export async function renderAndGetWindow (url, opts = {}) { /* istanbul ignore if */ if (!jsdom) { try { - // https://github.com/tmpvar/jsdom/blob/master/lib/old-api.md - jsdom = require('jsdom/lib/old-api') + jsdom = require('jsdom') } catch (e) { console.error('Fail when calling nuxt.renderAndGetWindow(url)') // eslint-disable-line no-console console.error('jsdom module is not installed') // eslint-disable-line no-console @@ -151,35 +150,29 @@ export function renderAndGetWindow (url, opts = {}) { process.exit(1) } } - let virtualConsole = jsdom.createVirtualConsole().sendTo(console) - // let virtualConsole = new jsdom.VirtualConsole().sendTo(console) - if (opts.virtualConsole === false) { - virtualConsole = undefined + let options = { + resources: 'usable', // load subresources (https://github.com/tmpvar/jsdom#loading-subresources) + runScripts: 'dangerously', + beforeParse(window) { + // Mock window.scrollTo + window.scrollTo = () => {} + } + } + if (opts.virtualConsole !== false) { + options.virtualConsole = new jsdom.VirtualConsole().sendTo(console) } url = url || 'http://localhost:3000' - return new Promise((resolve, reject) => { - jsdom.env({ - url: url, - features: { - FetchExternalResources: ['script', 'link'], - ProcessExternalResources: ['script'] - }, - virtualConsole, - done (err, window) { - if (err) return reject(err) - // Mock window.scrollTo - window.scrollTo = function () {} - // If Nuxt could not be loaded (error from the server-side) - if (!window.__NUXT__) { - let error = new Error('Could not load the nuxt app') - error.body = window.document.getElementsByTagName('body')[0].innerHTML - return reject(error) - } - // Used by nuxt.js to say when the components are loaded and the app ready - window.onNuxtReady(() => { - resolve(window) - }) - } - }) + const { window } = await jsdom.JSDOM.fromURL(url, options) + // If Nuxt could not be loaded (error from the server-side) + if (!window.__NUXT__) { + let error = new Error('Could not load the nuxt app') + error.body = window.document.getElementsByTagName('body')[0].innerHTML + throw error + } + // Used by nuxt.js to say when the components are loaded and the app ready + await new Promise((resolve) => { + window._onNuxtLoaded = () => resolve(window) }) + // Send back window object + return window }