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",
"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"
}
}

View File

@ -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')

View File

@ -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)

View File

@ -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 () {}
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
return reject(error)
throw error
}
// Used by nuxt.js to say when the components are loaded and the app ready
window.onNuxtReady(() => {
resolve(window)
})
}
})
await new Promise((resolve) => {
window._onNuxtLoaded = () => resolve(window)
})
// Send back window object
return window
}