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

@ -164,9 +164,10 @@ export async function renderAndGetWindow (url, opts = {}) {
url = url || 'http://localhost:3000' url = url || 'http://localhost:3000'
const { window } = await jsdom.JSDOM.fromURL(url, options) const { window } = await jsdom.JSDOM.fromURL(url, options)
// If Nuxt could not be loaded (error from the server-side) // 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') 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 throw error
} }
// Used by nuxt.js to say when the components are loaded and the app ready // 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 => { test('/ with renderAndGetWindow()', async t => {
try { const err = await t.throws(nuxt.renderAndGetWindow(url('/')))
await nuxt.renderAndGetWindow(url('/')) t.is(err.response.statusCode, 500)
} 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'))
}
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes

View File

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

View File

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

View File

@ -78,7 +78,7 @@ test('/test/error', async t => {
test('/test/user-agent', async t => { test('/test/user-agent', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/user-agent')) const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
const html = window.document.body.innerHTML 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 => { test('/test/about-bis (added with extendRoutes)', async t => {