diff --git a/examples/with-ava/test/index.test.js b/examples/with-ava/test/index.test.js index 75272b589d..c19d0a692c 100755 --- a/examples/with-ava/test/index.test.js +++ b/examples/with-ava/test/index.test.js @@ -2,7 +2,6 @@ ** Test with Ava can be written in ES6 \o/ */ import test from 'ava' -import jsdom from 'jsdom' import { createServer } from 'http' import { resolve } from 'path' @@ -39,7 +38,7 @@ test('Route / exits and render HTML', async t => { ** Example of testing via dom checking */ test('Route / exits and render HTML', async t => { - const window = await nuxt.renderAndGetWindow(jsdom, 'http://localhost:4000/') + const window = await nuxt.renderAndGetWindow('http://localhost:4000/') const element = window.document.querySelector('.red-color') t.not(element, null) t.is(element.textContent, 'Hello world!') diff --git a/lib/render.js b/lib/render.js index c6850d6d81..dcf01739ef 100644 --- a/lib/render.js +++ b/lib/render.js @@ -94,7 +94,18 @@ exports.renderRoute = function (url, context = {}) { } // Function used to do dom checking via jsdom -exports.renderAndGetWindow = function renderAndGetWindow (jsdom, url) { +let jsdom = null +exports.renderAndGetWindow = function renderAndGetWindow (url) { + if (!jsdom) { + try { + 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 + console.error('Please install jsdom with: npm install --save-dev jsdom') // eslint-disable-line no-console + process.exit(1) + } + } const virtualConsole = jsdom.createVirtualConsole().sendTo(console) url = url || 'http://localhost:3000' return new Promise((resolve, reject) => { diff --git a/test/basic.test.js b/test/basic.test.js index 184cedad2c..8af209908e 100755 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,5 +1,4 @@ import test from 'ava' -import jsdom from 'jsdom' import { resolve } from 'path' const port = 4002 const url = (route) => 'http://localhost:' + port + route @@ -31,7 +30,7 @@ test('/stateless', async t => { ** Example of testing via dom checking */ test('/css', async t => { - const window = await nuxt.renderAndGetWindow(jsdom, url('/css')) + const window = await nuxt.renderAndGetWindow(url('/css')) const element = window.document.querySelector('.red') t.not(element, null) t.is(element.textContent, 'This is red') @@ -45,7 +44,7 @@ test('/stateful', async t => { }) test('/head', async t => { - const window = await nuxt.renderAndGetWindow(jsdom, url('/head')) + const window = await nuxt.renderAndGetWindow(url('/head')) const html = window.document.body.innerHTML const metas = window.document.getElementsByTagName('meta') t.is(window.document.title, 'My title')