Update JSDOM logic

This commit is contained in:
Sebastien Chopin 2017-05-30 18:18:01 +02:00
parent 2bb5cca1d5
commit 979e15db2a
5 changed files with 9 additions and 12 deletions

View File

@ -153,7 +153,7 @@ export async function renderAndGetWindow (url, opts = {}) {
let options = {
resources: 'usable', // load subresources (https://github.com/tmpvar/jsdom#loading-subresources)
runScripts: 'dangerously',
beforeParse(window) {
beforeParse (window) {
// Mock window.scrollTo
window.scrollTo = () => {}
}
@ -164,9 +164,10 @@ export async function renderAndGetWindow (url, opts = {}) {
url = url || 'http://localhost:3000'
const { window } = await jsdom.JSDOM.fromURL(url, options)
// If Nuxt could not be loaded (error from the server-side)
if (!window.__NUXT__) {
const nuxtExists = window.document.body.innerHTML.includes('window.__NUXT__')
if (!nuxtExists) {
let error = new Error('Could not load the nuxt app')
error.body = window.document.getElementsByTagName('body')[0].innerHTML
error.body = window.document.body.innerHTML
throw error
}
// Used by nuxt.js to say when the components are loaded and the app ready

View File

@ -33,12 +33,8 @@ test('/404 should display an error too', async t => {
})
test('/ with renderAndGetWindow()', async t => {
try {
await nuxt.renderAndGetWindow(url('/'))
} catch (e) {
t.true(e.message.includes('Could not load the nuxt app'))
t.true(e.body.includes('render function or template not defined in component'))
}
const err = await t.throws(nuxt.renderAndGetWindow(url('/')))
t.is(err.response.statusCode, 500)
})
// Close server and ask nuxt to stop listening to file changes

View File

@ -4,7 +4,7 @@
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
{{ APP }}
<p>Made by Nuxt.js team</p>
{{ APP }}
</body>
</html>

View File

@ -5,7 +5,7 @@
<script>
export default {
fetch ({ error }) {
error({ message: 'Nuxt Error', statusCode: 300 })
error({ message: 'Nuxt Error', statusCode: 200 })
}
}
</script>

View File

@ -78,7 +78,7 @@ test('/test/error', async t => {
test('/test/user-agent', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
const html = window.document.body.innerHTML
t.true(html.includes('<pre>Node.js'))
t.true(html.includes('<pre>Mozilla'))
})
test('/test/about-bis (added with extendRoutes)', async t => {