Upgrade dependencies and JSDOM API

This commit is contained in:
Sebastien Chopin 2017-05-30 16:08:51 +02:00
parent 65c07a6bc7
commit 488010bf78
4 changed files with 33 additions and 36 deletions

View File

@ -4,13 +4,13 @@
"dev": "nuxt", "dev": "nuxt",
"build": "nuxt build", "build": "nuxt build",
"start": "nuxt start", "start": "nuxt start",
"test": "ava" "test": "ava --serial --verbose"
}, },
"devDependencies": { "devDependencies": {
"ava": "^0.16.0", "ava": "^0.19.1",
"jsdom": "^9.8.3" "jsdom": "^11.0.0"
}, },
"dependencies": { "dependencies": {
"nuxt": "^0.9.5" "nuxt": "^1.0.0-alpha2"
} }
} }

View File

@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => {
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {} try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
config.rootDir = rootDir // project folder config.rootDir = rootDir // project folder
config.dev = false // production build config.dev = false // production build
nuxt = new Nuxt(config) nuxt = await new Nuxt(config)
await nuxt.build() await nuxt.build()
server = new nuxt.Server(nuxt) server = new nuxt.Server(nuxt)
server.listen(4000, 'localhost') server.listen(4000, 'localhost')

View File

@ -349,6 +349,10 @@ function nuxtReady (app) {
cb(app) cb(app)
} }
}) })
// Special JSDOM
if (typeof window._onNuxtLoaded === 'function') {
window._onNuxtLoaded(app)
}
// Add router hooks // Add router hooks
router.afterEach(function (to, from) { router.afterEach(function (to, from) {
app.$nuxt.$emit('routeChanged', to, from) app.$nuxt.$emit('routeChanged', to, from)

View File

@ -138,12 +138,11 @@ export async function renderRoute (url, context = {}) {
// Function used to do dom checking via jsdom // Function used to do dom checking via jsdom
let jsdom = null let jsdom = null
export function renderAndGetWindow (url, opts = {}) { export async function renderAndGetWindow (url, opts = {}) {
/* istanbul ignore if */ /* istanbul ignore if */
if (!jsdom) { if (!jsdom) {
try { try {
// https://github.com/tmpvar/jsdom/blob/master/lib/old-api.md jsdom = require('jsdom')
jsdom = require('jsdom/lib/old-api')
} catch (e) { } catch (e) {
console.error('Fail when calling nuxt.renderAndGetWindow(url)') // eslint-disable-line no-console 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 console.error('jsdom module is not installed') // eslint-disable-line no-console
@ -151,35 +150,29 @@ export function renderAndGetWindow (url, opts = {}) {
process.exit(1) process.exit(1)
} }
} }
let virtualConsole = jsdom.createVirtualConsole().sendTo(console) let options = {
// let virtualConsole = new jsdom.VirtualConsole().sendTo(console) resources: 'usable', // load subresources (https://github.com/tmpvar/jsdom#loading-subresources)
if (opts.virtualConsole === false) { runScripts: 'dangerously',
virtualConsole = undefined beforeParse(window) {
// Mock window.scrollTo
window.scrollTo = () => {}
}
}
if (opts.virtualConsole !== false) {
options.virtualConsole = new jsdom.VirtualConsole().sendTo(console)
} }
url = url || 'http://localhost:3000' url = url || 'http://localhost:3000'
return new Promise((resolve, reject) => { const { window } = await jsdom.JSDOM.fromURL(url, options)
jsdom.env({ // If Nuxt could not be loaded (error from the server-side)
url: url, if (!window.__NUXT__) {
features: { let error = new Error('Could not load the nuxt app')
FetchExternalResources: ['script', 'link'], error.body = window.document.getElementsByTagName('body')[0].innerHTML
ProcessExternalResources: ['script'] throw error
}, }
virtualConsole, // Used by nuxt.js to say when the components are loaded and the app ready
done (err, window) { await new Promise((resolve) => {
if (err) return reject(err) window._onNuxtLoaded = () => resolve(window)
// 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)
})
}
})
}) })
// Send back window object
return window
} }