Use console helper with sinon

Add stdout/stderr to console helpers

Remove separate nosubfolders test file in favor of inclusion in basic.generate

Add build.stats to silence webpack output

Add .always to after hooks: (1) fixes issue with basic.dev test that watch.js is empty when one of the test fails and (2) fixes that sometimes when running multiple tests that failed after eachother the port is still used as the server did not automatically get killed

Change all init nuxt before test to serial tests and add tests for DONE and OPEN logs
This commit is contained in:
pimlie 2017-12-17 20:30:26 +01:00
parent c92d427bb0
commit fffe741986
30 changed files with 543 additions and 456 deletions

2
.gitignore vendored
View File

@ -9,7 +9,7 @@ package-lock.json
npm-debug.log* npm-debug.log*
# Other # Other
.nuxt .nuxt*
.cache .cache
# Dist folder # Dist folder

View File

@ -45,7 +45,7 @@
] ]
}, },
"scripts": { "scripts": {
"test": "npm run lint && nyc ava --verbose --serial test/ -- && nyc report --reporter=html", "test": "npm run lint && nyc ava --verbose test/ -- && nyc report --reporter=html",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"lint": "eslint --ext .js,.vue bin/* build/ lib/ test/ examples/", "lint": "eslint --ext .js,.vue bin/* build/ lib/ test/ examples/",
"precommit": "npm run lint", "precommit": "npm run lint",

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import * as browser from './helpers/browser' import * as browser from './helpers/browser'
import { interceptLog } from './helpers/console'
const port = 4003 const port = 4003
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -10,36 +11,46 @@ let nuxt = null
let page = null let page = null
// Init nuxt.js and create server listening on localhost:4003 // Init nuxt.js and create server listening on localhost:4003
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/basic'), rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false, buildDir: '.nuxt-csr',
dev: true,
head: { head: {
titleTemplate(titleChunk) { titleTemplate(titleChunk) {
return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js' return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js'
} }
},
build: {
stats: false
} }
} }
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test.before('Start browser', async t => { test.serial('Start browser', async t => {
t.plan(0) // suppress 'no assertions' warning
await browser.start({ await browser.start({
// slowMo: 50, // slowMo: 50,
// headless: false // headless: false
}) })
}) })
test('Open /', async t => { test.serial('Open /', async t => {
page = await browser.page(url('/')) page = await browser.page(url('/'))
t.is(await page.$text('h1'), 'Index page') t.is(await page.$text('h1'), 'Index page')
}) })
test('/stateless', async t => { test.serial('/stateless', async t => {
const { hook } = await page.nuxt.navigate('/stateless', false) const { hook } = await page.nuxt.navigate('/stateless', false)
const loading = await page.nuxt.loadingData() const loading = await page.nuxt.loadingData()
@ -48,27 +59,27 @@ test('/stateless', async t => {
t.is(await page.$text('h1'), 'My component!') t.is(await page.$text('h1'), 'My component!')
}) })
test('/css', async t => { test.serial('/css', async t => {
await page.nuxt.navigate('/css') await page.nuxt.navigate('/css')
t.is(await page.$text('.red'), 'This is red') t.is(await page.$text('.red'), 'This is red')
t.is(await page.$eval('.red', (red) => window.getComputedStyle(red).color), 'rgb(255, 0, 0)') t.is(await page.$eval('.red', (red) => window.getComputedStyle(red).color), 'rgb(255, 0, 0)')
}) })
test('/stateful', async t => { test.serial('/stateful', async t => {
await page.nuxt.navigate('/stateful') await page.nuxt.navigate('/stateful')
t.is(await page.$text('p'), 'The answer is 42') t.is(await page.$text('p'), 'The answer is 42')
}) })
test('/store', async t => { test.serial('/store', async t => {
await page.nuxt.navigate('/store') await page.nuxt.navigate('/store')
t.is(await page.$text('h1'), 'Vuex Nested Modules') t.is(await page.$text('h1'), 'Vuex Nested Modules')
t.is(await page.$text('p'), '1') t.is(await page.$text('p'), '1')
}) })
test('/head', async t => { test.serial('/head', async t => {
const msg = new Promise((resolve) => page.on('console', (msg) => resolve(msg.text))) const msg = new Promise((resolve) => page.on('console', (msg) => resolve(msg.text)))
await page.nuxt.navigate('/head') await page.nuxt.navigate('/head')
const metas = await page.$$attr('meta', 'content') const metas = await page.$$attr('meta', 'content')
@ -79,31 +90,31 @@ test('/head', async t => {
t.is(metas[0], 'my meta') t.is(metas[0], 'my meta')
}) })
test('/async-data', async t => { test.serial('/async-data', async t => {
await page.nuxt.navigate('/async-data') await page.nuxt.navigate('/async-data')
t.is(await page.$text('p'), 'Nuxt.js') t.is(await page.$text('p'), 'Nuxt.js')
}) })
test('/await-async-data', async t => { test.serial('/await-async-data', async t => {
await page.nuxt.navigate('/await-async-data') await page.nuxt.navigate('/await-async-data')
t.is(await page.$text('p'), 'Await Nuxt.js') t.is(await page.$text('p'), 'Await Nuxt.js')
}) })
test('/callback-async-data', async t => { test.serial('/callback-async-data', async t => {
await page.nuxt.navigate('/callback-async-data') await page.nuxt.navigate('/callback-async-data')
t.is(await page.$text('p'), 'Callback Nuxt.js') t.is(await page.$text('p'), 'Callback Nuxt.js')
}) })
test('/users/1', async t => { test.serial('/users/1', async t => {
await page.nuxt.navigate('/users/1') await page.nuxt.navigate('/users/1')
t.is(await page.$text('h1'), 'User: 1') t.is(await page.$text('h1'), 'User: 1')
}) })
test('/validate should display a 404', async t => { test.serial('/validate should display a 404', async t => {
await page.nuxt.navigate('/validate') await page.nuxt.navigate('/validate')
const error = await page.nuxt.errorData() const error = await page.nuxt.errorData()
@ -111,39 +122,39 @@ test('/validate should display a 404', async t => {
t.is(error.message, 'This page could not be found') t.is(error.message, 'This page could not be found')
}) })
test('/validate?valid=true', async t => { test.serial('/validate?valid=true', async t => {
await page.nuxt.navigate('/validate?valid=true') await page.nuxt.navigate('/validate?valid=true')
t.is(await page.$text('h1'), 'I am valid') t.is(await page.$text('h1'), 'I am valid')
}) })
test('/redirect', async t => { test.serial('/redirect', async t => {
await page.nuxt.navigate('/redirect') await page.nuxt.navigate('/redirect')
t.is(await page.$text('h1'), 'Index page') t.is(await page.$text('h1'), 'Index page')
}) })
test('/error', async t => { test.serial('/error', async t => {
await page.nuxt.navigate('/error') await page.nuxt.navigate('/error')
t.deepEqual(await page.nuxt.errorData(), { statusCode: 500 }) t.deepEqual(await page.nuxt.errorData(), { statusCode: 500 })
t.is(await page.$text('.title'), 'Error mouahahah') t.is(await page.$text('.title'), 'Error mouahahah')
}) })
test('/error2', async t => { test.serial('/error2', async t => {
await page.nuxt.navigate('/error2') await page.nuxt.navigate('/error2')
t.is(await page.$text('.title'), 'Custom error') t.is(await page.$text('.title'), 'Custom error')
t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' }) t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' })
}) })
test('/redirect2', async t => { test.serial('/redirect2', async t => {
await page.nuxt.navigate('/redirect2') await page.nuxt.navigate('/redirect2')
t.is(await page.$text('h1'), 'Index page') t.is(await page.$text('h1'), 'Index page')
}) })
test('/redirect3', async t => { test.serial('/redirect3', async t => {
// New page for redirecting to external link. // New page for redirecting to external link.
const page = await browser.page(url('/')) const page = await browser.page(url('/'))
await page.nuxt.navigate('/redirect3', false) await page.nuxt.navigate('/redirect3', false)
@ -152,44 +163,44 @@ test('/redirect3', async t => {
t.pass() t.pass()
}) })
test('/no-ssr', async t => { test.serial('/no-ssr', async t => {
await page.nuxt.navigate('/no-ssr') await page.nuxt.navigate('/no-ssr')
t.is(await page.$text('h1'), 'Displayed only on client-side') t.is(await page.$text('h1'), 'Displayed only on client-side')
}) })
test('/meta', async t => { test.serial('/meta', async t => {
await page.nuxt.navigate('/meta') await page.nuxt.navigate('/meta')
const state = await page.nuxt.storeState() const state = await page.nuxt.storeState()
t.deepEqual(state.meta, [{ works: true }]) t.deepEqual(state.meta, [{ works: true }])
}) })
test('/fn-midd', async t => { test.serial('/fn-midd', async t => {
await page.nuxt.navigate('/fn-midd') await page.nuxt.navigate('/fn-midd')
t.is(await page.$text('.title'), 'You need to ask the permission') t.is(await page.$text('.title'), 'You need to ask the permission')
t.deepEqual(await page.nuxt.errorData(), { message: 'You need to ask the permission', statusCode: 403 }) t.deepEqual(await page.nuxt.errorData(), { message: 'You need to ask the permission', statusCode: 403 })
}) })
test('/fn-midd?please=true', async t => { test.serial('/fn-midd?please=true', async t => {
await page.nuxt.navigate('/fn-midd?please=true') await page.nuxt.navigate('/fn-midd?please=true')
const h1 = await page.$text('h1') const h1 = await page.$text('h1')
t.true(h1.includes('Date:')) t.true(h1.includes('Date:'))
}) })
test('/router-guard', async t => { test.serial('/router-guard', async t => {
await page.nuxt.navigate('/router-guard') await page.nuxt.navigate('/router-guard')
t.is(await page.$text('p'), 'Nuxt.js') t.is(await page.$text('p'), 'Nuxt.js')
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })
test.after('Stop browser', async t => { test.after.always('Stop browser', async t => {
await browser.stop() await browser.stop()
}) })

View File

@ -1,6 +1,6 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import { interceptLog, release } from './helpers/console' import { intercept, release } from './helpers/console'
import { Nuxt, Builder, Utils } from '..' import { Nuxt, Builder, Utils } from '..'
import { truncateSync, readFileSync, writeFileSync } from 'fs' import { truncateSync, readFileSync, writeFileSync } from 'fs'
@ -13,32 +13,39 @@ const pluginContent = readFileSync(pluginPath)
let nuxt = null let nuxt = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const options = {
rootDir, rootDir,
buildDir: '.nuxt-dev',
dev: true, dev: true,
build: { build: {
stats: false,
profile: true profile: true
}, },
plugins: [ plugins: [
'~/plugins/watch.js' '~/plugins/watch.js'
] ]
} }
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') const spies = await intercept({ log: true, stderr: true }, async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(spies.log.calledWithMatch('DONE'))
t.true(spies.log.calledWithMatch('OPEN'))
}) })
test('remove mixins in live reloading', async t => { test.serial('remove mixins in live reloading', async t => {
const logSpy = await interceptLog() const spies = await intercept()
await nuxt.renderRoute(url('/')) await nuxt.renderRoute(url('/'))
t.true(logSpy.calledWith('I am mixin')) t.true(spies.log.calledWith('I am mixin'))
truncateSync(pluginPath) truncateSync(pluginPath)
await new Promise(async (resolve, reject) => { await new Promise(async (resolve, reject) => {
let waitTimes = 0 let waitTimes = 0
while (logSpy.neverCalledWithMatch(/Compiled successfully/)) { while (spies.log.neverCalledWithMatch(/Compiled successfully/)) {
if (waitTimes++ >= 20) { if (waitTimes++ >= 20) {
t.fail('Dev server doesn\'t reload after 2000ms') t.fail('Dev server doesn\'t reload after 2000ms')
reject(Error()) reject(Error())
@ -47,16 +54,21 @@ test('remove mixins in live reloading', async t => {
} }
resolve() resolve()
}) })
logSpy.reset() spies.log.reset()
await nuxt.renderRoute(url('/')) await nuxt.renderRoute(url('/'))
t.true(logSpy.neverCalledWith('I am mixin')) t.true(spies.log.neverCalledWith('I am mixin'))
t.is(spies.error.getCall(0).args[0].statusCode, 404)
release() release()
}) })
test('/stateless', async t => { test.serial('/stateless', async t => {
const spies = await intercept()
const window = await nuxt.renderAndGetWindow(url('/stateless')) const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<h1>My component!</h1>')) t.true(html.includes('<h1>My component!</h1>'))
t.true(spies.info.calledWithMatch('You are running Vue in development mode.'))
release()
}) })
// test('/_nuxt/test.hot-update.json should returns empty html', async t => { // test('/_nuxt/test.hot-update.json should returns empty html', async t => {
@ -69,7 +81,7 @@ test('/stateless', async t => {
// }) // })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', async t => { test.after.always('Closing server and nuxt.js', async t => {
writeFileSync(pluginPath, pluginContent) writeFileSync(pluginPath, pluginContent)
await nuxt.close() await nuxt.close()
}) })

View File

@ -1,22 +1,32 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import { Nuxt, Builder, Generator } from '..' import { Nuxt, Builder, Generator } from '..'
import { intercept } from './helpers/console'
test('Fail with routes() which throw an error', async t => { test('Fail with routes() which throw an error', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/basic'), rootDir: resolve(__dirname, 'fixtures/basic'),
buildDir: '.nuxt-fail',
dev: false, dev: false,
build: {
stats: false
},
generate: { generate: {
async routes() { async routes() {
throw new Error('Not today!') throw new Error('Not today!')
} }
} }
} }
const nuxt = new Nuxt(options) const spies = await intercept(async () => {
const builder = new Builder(nuxt) const nuxt = new Nuxt(options)
const generator = new Generator(nuxt, builder) const builder = new Builder(nuxt)
return generator.generate() const generator = new Generator(nuxt, builder)
.catch((e) => {
t.true(e.message === 'Not today!') return generator.generate()
}) .catch((e) => {
t.true(e.message === 'Not today!')
})
})
t.true(spies.log.calledWithMatch('DONE'))
t.true(spies.error.withArgs('Could not resolve routes').calledOnce)
}) })

View File

@ -1,138 +0,0 @@
import test from 'ava'
import { resolve } from 'path'
import { existsSync } from 'fs'
import http from 'http'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import rp from 'request-promise-native'
import { Nuxt, Builder, Generator } from '..'
const port = 4002
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
let server = null
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/basic')
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
config.generate.subFolders = false
nuxt = new Nuxt(config)
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
try {
await generator.generate() // throw an error (of /validate route)
} catch (err) {
}
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'), { extensions: ['html'] })
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
})
server.listen(port)
})
test('Check ready hook called', async t => {
t.true(nuxt.__hook_called__)
})
test('/stateless', async t => {
const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>My component!</h1>'))
})
test('/css', async t => {
const window = await nuxt.renderAndGetWindow(url('/css'))
const element = window.document.querySelector('.red')
t.not(element, null)
t.is(element.textContent, 'This is red')
t.is(element.className, 'red')
t.is(window.getComputedStyle(element).color, 'red')
})
test('/stateful', async t => {
const window = await nuxt.renderAndGetWindow(url('/stateful'))
const html = window.document.body.innerHTML
t.true(html.includes('<div><p>The answer is 42</p></div>'))
})
test('/head', async t => {
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')
t.is(metas[0].getAttribute('content'), 'my meta')
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
})
test('/async-data', async t => {
const window = await nuxt.renderAndGetWindow(url('/async-data'))
const html = window.document.body.innerHTML
t.true(html.includes('<p>Nuxt.js</p>'))
})
test('/users/1', async t => {
const html = await rp(url('/users/1'))
t.true(html.includes('<h1>User: 1</h1>'))
t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html')))
t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html')))
})
test('/users/2', async t => {
const html = await rp(url('/users/2'))
t.true(html.includes('<h1>User: 2</h1>'))
})
test('/users/3 (payload given)', async t => {
const html = await rp(url('/users/3'))
t.true(html.includes('<h1>User: 3000</h1>'))
})
test('/users/4 -> Not found', async t => {
try {
await rp(url('/users/4'))
} catch (error) {
t.true(error.statusCode === 404)
t.true(error.response.body.includes('Cannot GET /users/4'))
}
})
test('/validate should not be server-rendered', async t => {
const html = await rp(url('/validate'))
t.true(html.includes('<div id="__nuxt"></div>'))
t.true(html.includes('serverRendered:!1'))
})
test('/validate -> should display a 404', async t => {
const window = await nuxt.renderAndGetWindow(url('/validate'))
const html = window.document.body.innerHTML
t.true(html.includes('This page could not be found'))
})
test('/validate?valid=true', async t => {
const window = await nuxt.renderAndGetWindow(url('/validate?valid=true'))
const html = window.document.body.innerHTML
t.true(html.includes('I am valid</h1>'))
})
test('/redirect should not be server-rendered', async t => {
const html = await rp(url('/redirect'))
t.true(html.includes('<div id="__nuxt"></div>'))
t.true(html.includes('serverRendered:!1'))
})
test('/redirect -> check redirected source', async t => {
const window = await nuxt.renderAndGetWindow(url('/redirect'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>Index page</h1>'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server', t => {
server.close()
})

View File

@ -1,31 +1,39 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { remove } from 'fs-extra'
import http from 'http' import http from 'http'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import finalhandler from 'finalhandler' import finalhandler from 'finalhandler'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { interceptLog, release } from './helpers/console'
import { Nuxt, Builder, Generator } from '..' import { Nuxt, Builder, Generator } from '..'
const port = 4002 const port = 4002
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
const rootDir = resolve(__dirname, 'fixtures/basic')
let nuxt = null let nuxt = null
let server = null let server = null
let generator = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/basic')
let config = require(resolve(rootDir, 'nuxt.config.js')) let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir config.rootDir = rootDir
config.buildDir = '.nuxt-generate'
config.dev = false config.dev = false
nuxt = new Nuxt(config) config.build.stats = false
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder) const logSpy = await interceptLog(async () => {
try { nuxt = new Nuxt(config)
await generator.generate() // throw an error (of /validate route) const builder = new Builder(nuxt)
} catch (err) { generator = new Generator(nuxt, builder)
}
await generator.generate()
})
t.true(logSpy.calledWithMatch('DONE'))
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer((req, res) => { server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res)) serve(req, res, finalhandler(req, res))
@ -33,17 +41,17 @@ test.before('Init Nuxt.js', async t => {
server.listen(port) server.listen(port)
}) })
test('Check ready hook called', async t => { test.serial('Check ready hook called', async t => {
t.true(nuxt.__hook_called__) t.true(nuxt.__hook_called__)
}) })
test('/stateless', async t => { test.serial('/stateless', async t => {
const window = await nuxt.renderAndGetWindow(url('/stateless')) const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<h1>My component!</h1>')) t.true(html.includes('<h1>My component!</h1>'))
}) })
test('/css', async t => { test.serial('/css', async t => {
const window = await nuxt.renderAndGetWindow(url('/css')) const window = await nuxt.renderAndGetWindow(url('/css'))
const element = window.document.querySelector('.red') const element = window.document.querySelector('.red')
t.not(element, null) t.not(element, null)
@ -52,84 +60,106 @@ test('/css', async t => {
t.is(window.getComputedStyle(element).color, 'red') t.is(window.getComputedStyle(element).color, 'red')
}) })
test('/stateful', async t => { test.serial('/stateful', async t => {
const window = await nuxt.renderAndGetWindow(url('/stateful')) const window = await nuxt.renderAndGetWindow(url('/stateful'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<div><p>The answer is 42</p></div>')) t.true(html.includes('<div><p>The answer is 42</p></div>'))
}) })
test('/head', async t => { test.serial('/head', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/head')) const window = await nuxt.renderAndGetWindow(url('/head'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
const metas = window.document.getElementsByTagName('meta') const metas = window.document.getElementsByTagName('meta')
t.is(window.document.title, 'My title') t.is(window.document.title, 'My title')
t.is(metas[0].getAttribute('content'), 'my meta') t.is(metas[0].getAttribute('content'), 'my meta')
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>')) t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
release()
t.is(logSpy.getCall(0).args[0], 'Body script!')
}) })
test('/async-data', async t => { test.serial('/async-data', async t => {
const window = await nuxt.renderAndGetWindow(url('/async-data')) const window = await nuxt.renderAndGetWindow(url('/async-data'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<p>Nuxt.js</p>')) t.true(html.includes('<p>Nuxt.js</p>'))
}) })
test('/users/1', async t => { test.serial('/users/1/index.html', async t => {
const html = await rp(url('/users/1')) const html = await rp(url('/users/1/index.html'))
t.true(html.includes('<h1>User: 1</h1>')) t.true(html.includes('<h1>User: 1</h1>'))
t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))) t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html')))
t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html'))) t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html')))
}) })
test('/users/2', async t => { test.serial('/users/2', async t => {
const html = await rp(url('/users/2')) const html = await rp(url('/users/2'))
t.true(html.includes('<h1>User: 2</h1>')) t.true(html.includes('<h1>User: 2</h1>'))
}) })
test('/users/3 (payload given)', async t => { test.serial('/users/3 (payload given)', async t => {
const html = await rp(url('/users/3')) const html = await rp(url('/users/3'))
t.true(html.includes('<h1>User: 3000</h1>')) t.true(html.includes('<h1>User: 3000</h1>'))
}) })
test('/users/4 -> Not found', async t => { test.serial('/users/4 -> Not found', async t => {
try { const error = await t.throws(rp(url('/users/4')))
await rp(url('/users/4')) t.true(error.statusCode === 404)
} catch (error) { t.true(error.response.body.includes('Cannot GET /users/4'))
t.true(error.statusCode === 404)
t.true(error.response.body.includes('Cannot GET /users/4'))
}
}) })
test('/validate should not be server-rendered', async t => { test.serial('/validate should not be server-rendered', async t => {
const html = await rp(url('/validate')) const html = await rp(url('/validate'))
t.true(html.includes('<div id="__nuxt"></div>')) t.true(html.includes('<div id="__nuxt"></div>'))
t.true(html.includes('serverRendered:!1')) t.true(html.includes('serverRendered:!1'))
}) })
test('/validate -> should display a 404', async t => { test.serial('/validate -> should display a 404', async t => {
const window = await nuxt.renderAndGetWindow(url('/validate')) const window = await nuxt.renderAndGetWindow(url('/validate'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('This page could not be found')) t.true(html.includes('This page could not be found'))
}) })
test('/validate?valid=true', async t => { test.serial('/validate?valid=true', async t => {
const window = await nuxt.renderAndGetWindow(url('/validate?valid=true')) const window = await nuxt.renderAndGetWindow(url('/validate?valid=true'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('I am valid</h1>')) t.true(html.includes('I am valid</h1>'))
}) })
test('/redirect should not be server-rendered', async t => { test.serial('/redirect should not be server-rendered', async t => {
const html = await rp(url('/redirect')) const html = await rp(url('/redirect'))
t.true(html.includes('<div id="__nuxt"></div>')) t.true(html.includes('<div id="__nuxt"></div>'))
t.true(html.includes('serverRendered:!1')) t.true(html.includes('serverRendered:!1'))
}) })
test('/redirect -> check redirected source', async t => { test.serial('/redirect -> check redirected source', async t => {
const window = await nuxt.renderAndGetWindow(url('/redirect')) const window = await nuxt.renderAndGetWindow(url('/redirect'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<h1>Index page</h1>')) t.true(html.includes('<h1>Index page</h1>'))
}) })
// Close server and ask nuxt to stop listening to file changes test.serial('/users/1 not found', async t => {
test.after('Closing server', t => { await remove(resolve(rootDir, 'dist/users'))
server.close() const error = await t.throws(rp(url('/users/1')))
t.true(error.statusCode === 404)
t.true(error.response.body.includes('Cannot GET /users/1'))
})
test.serial('nuxt re-generating with no subfolders', async t => {
const logSpy = await interceptLog()
nuxt.options.generate.subFolders = false
await generator.generate()
release()
t.true(logSpy.calledWithMatch('DONE'))
})
test.serial('/users/1.html', async t => {
const html = await rp(url('/users/1.html'))
t.true(html.includes('<h1>User: 1</h1>'))
t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html')))
t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html')))
})
// Close server and ask nuxt to stop listening to file changes
test.after.always('Closing server', async t => {
await server.close()
}) })

View File

@ -2,30 +2,38 @@ import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { interceptLog, interceptError } from './helpers/console' import { interceptLog, interceptError, release } from './helpers/console'
const port = 4003 const port = 4004
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
// Init nuxt.js and create server listening on localhost:4003 // Init nuxt.js and create server listening on localhost:4003
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/basic'), rootDir: resolve(__dirname, 'fixtures/basic'),
buildDir: '.nuxt-ssr',
dev: false, dev: false,
head: { head: {
titleTemplate(titleChunk) { titleTemplate(titleChunk) {
return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js' return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js'
} }
},
build: {
stats: false
} }
} }
await interceptLog('building nuxt', async () => { const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options) nuxt = new Nuxt(options)
await new Builder(nuxt).build() const builder = await new Builder(nuxt)
await nuxt.listen(port, 'localhost') await builder.build()
await nuxt.listen(port, '0.0.0.0')
}) })
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test('/stateless', async t => { test('/stateless', async t => {
@ -62,18 +70,19 @@ test('/store', async t => {
t.true(html.includes('<p>1</p>')) t.true(html.includes('<p>1</p>'))
}) })
test('/head', async t => { test.serial('/head', async t => {
const logSpy = await interceptLog(async () => { const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false }) const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false })
t.is(window.document.title, 'My title - Nuxt.js') t.is(window.document.title, 'My title - Nuxt.js')
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>')) t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
t.true(html.includes('<script data-n-head="true" src="/body.js" data-body="true">')) t.true(html.includes('<script data-n-head="true" src="/body.js" data-body="true">'))
const metas = window.document.getElementsByTagName('meta')
t.is(metas[0].getAttribute('content'), 'my meta')
release()
const metas = window.document.getElementsByTagName('meta')
t.is(metas[0].getAttribute('content'), 'my meta')
})
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Body script!') t.is(logSpy.args[0][0], 'Body script!')
}) })
@ -145,12 +154,12 @@ test('/error', async t => {
t.true(err.message.includes('Error mouahahah')) t.true(err.message.includes('Error mouahahah'))
}) })
test('/error status code', async t => { test.serial('/error status code', async t => {
const errorSpy = await interceptError(async () => { const errorSpy = await interceptError()
const err = await t.throws(rp(url('/error'))) const err = await t.throws(rp(url('/error')))
t.true(err.statusCode === 500) t.true(err.statusCode === 500)
t.true(err.response.body.includes('An error occurred in the application and your page could not be served')) t.true(err.response.body.includes('An error occurred in the application and your page could not be served'))
}) release()
t.true(errorSpy.calledOnce) t.true(errorSpy.calledOnce)
t.true(errorSpy.args[0][0].message.includes('Error mouahahah')) t.true(errorSpy.args[0][0].message.includes('Error mouahahah'))
}) })
@ -163,29 +172,25 @@ test('/error2', async t => {
}) })
test('/error2 status code', async t => { test('/error2 status code', async t => {
try { const error = await t.throws(rp(url('/error2')))
await rp(url('/error2')) t.is(error.statusCode, 500)
} catch (err) { t.true(error.response.body.includes('Custom error'))
t.is(err.statusCode, 500)
t.true(err.response.body.includes('Custom error'))
}
}) })
test('/error-midd', async t => { test.serial('/error-midd', async t => {
const errorSpy = await interceptError(async () => { const errorSpy = await interceptError()
const err = await t.throws(rp(url('/error-midd'))) const err = await t.throws(rp(url('/error-midd')))
t.is(err.statusCode, 505)
t.is(err.statusCode, 505) t.true(err.response.body.includes('Middleware Error'))
t.true(err.response.body.includes('Middleware Error')) release()
})
// Don't display error since redirect returns a noopApp // Don't display error since redirect returns a noopApp
t.true(errorSpy.notCalled) t.true(errorSpy.notCalled)
}) })
test('/redirect2', async t => { test.serial('/redirect2', async t => {
const errorSpy = await interceptError(async () => { const errorSpy = await interceptError()
await rp(url('/redirect2')) // Should not console.error await rp(url('/redirect2')) // Should not console.error
}) release()
// Don't display error since redirect returns a noopApp // Don't display error since redirect returns a noopApp
t.true(errorSpy.notCalled) t.true(errorSpy.notCalled)
}) })
@ -202,16 +207,12 @@ test('/no-ssr (client-side)', async t => {
}) })
test('ETag Header', async t => { test('ETag Header', async t => {
const errorSpy = await interceptError(async () => { const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true })
const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true }) // Validate etag
// Validate etag t.regex(etag, /W\/".*"$/)
t.regex(etag, /W\/".*"$/) // Verify functionality
// Verify functionality const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } }))
const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } })) t.is(error.statusCode, 304)
t.is(error.statusCode, 304)
})
t.true(errorSpy.calledOnce)
t.true(errorSpy.args[0][0].includes('TypeError: Cannot read property \'split\' of undefined'))
}) })
test('/_nuxt/server-bundle.json should return 404', async t => { test('/_nuxt/server-bundle.json should return 404', async t => {
@ -226,7 +227,6 @@ test('/_nuxt/ should return 404', async t => {
test('/meta', async t => { test('/meta', async t => {
const { html } = await nuxt.renderRoute('/meta') const { html } = await nuxt.renderRoute('/meta')
t.true(html.includes('"meta":[{"works":true}]')) t.true(html.includes('"meta":[{"works":true}]'))
}) })
@ -258,6 +258,6 @@ test('/js-link', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', async t => { test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close() await nuxt.close()
}) })

View File

@ -2,34 +2,45 @@ import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import { Nuxt, Builder, Utils } from '..' import { Nuxt, Builder, Utils } from '..'
import * as browser from './helpers/browser' import * as browser from './helpers/browser'
import { interceptLog } from './helpers/console'
const port = 4005 const port = 4014
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let page
const dates = {}
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/children'), rootDir: resolve(__dirname, 'fixtures/children'),
dev: false buildDir: '.nuxt-patch',
dev: false,
build: {
stats: false
}
} }
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test.before('Start browser', async t => {
test.serial('Start browser', async t => {
t.plan(0) // suppress 'no assertions' warning
await browser.start({ await browser.start({
// slowMo: 50, // slowMo: 50,
// headless: false // headless: false
}) })
}) })
let page test.serial('Loading /patch and keep ', async t => {
const dates = {}
test('Loading /patch and keep ', async t => {
page = await browser.page(url('/patch')) page = await browser.page(url('/patch'))
const h1 = await page.$text('h1') const h1 = await page.$text('h1')
@ -39,7 +50,7 @@ test('Loading /patch and keep ', async t => {
dates.patch = await page.$text('[data-date-patch]') dates.patch = await page.$text('[data-date-patch]')
}) })
test('Navigate to /patch/1', async t => { test.serial('Navigate to /patch/1', async t => {
const { hook } = await page.nuxt.navigate('/patch/1', false) const { hook } = await page.nuxt.navigate('/patch/1', false)
const loading = await page.nuxt.loadingData() const loading = await page.nuxt.loadingData()
t.is(loading.show, true) t.is(loading.show, true)
@ -52,7 +63,7 @@ test('Navigate to /patch/1', async t => {
t.is(dates.patch, await page.$text('[data-date-patch]')) t.is(dates.patch, await page.$text('[data-date-patch]'))
}) })
test('Navigate to /patch/2', async t => { test.serial('Navigate to /patch/2', async t => {
await page.nuxt.navigate('/patch/2') await page.nuxt.navigate('/patch/2')
const date = await page.$text('[data-date-id]') const date = await page.$text('[data-date-id]')
@ -62,19 +73,19 @@ test('Navigate to /patch/2', async t => {
dates.id = date dates.id = date
}) })
test('Navigate to /patch/2?test=true', async t => { test.serial('Navigate to /patch/2?test=true', async t => {
await page.nuxt.navigate('/patch/2?test=true') await page.nuxt.navigate('/patch/2?test=true')
t.is(dates.patch, await page.$text('[data-date-patch]')) t.is(dates.patch, await page.$text('[data-date-patch]'))
t.is(dates.id, await page.$text('[data-date-id]')) t.is(dates.id, await page.$text('[data-date-id]'))
}) })
test('Navigate to /patch/2#test', async t => { test.serial('Navigate to /patch/2#test', async t => {
await page.nuxt.navigate('/patch/2#test') await page.nuxt.navigate('/patch/2#test')
t.is(dates.patch, await page.$text('[data-date-patch]')) t.is(dates.patch, await page.$text('[data-date-patch]'))
t.is(dates.id, await page.$text('[data-date-id]')) t.is(dates.id, await page.$text('[data-date-id]'))
}) })
test('Navigate to /patch/2/child', async t => { test.serial('Navigate to /patch/2/child', async t => {
await page.nuxt.navigate('/patch/2/child') await page.nuxt.navigate('/patch/2/child')
dates.child = await page.$text('[data-date-child]') dates.child = await page.$text('[data-date-child]')
dates.slug = await page.$text('[data-date-child-slug]') dates.slug = await page.$text('[data-date-child-slug]')
@ -85,7 +96,7 @@ test('Navigate to /patch/2/child', async t => {
t.true(+dates.slug > +dates.child) t.true(+dates.slug > +dates.child)
}) })
test('Navigate to /patch/2/child/1', async t => { test.serial('Navigate to /patch/2/child/1', async t => {
await page.nuxt.navigate('/patch/2/child/1') await page.nuxt.navigate('/patch/2/child/1')
const date = await page.$text('[data-date-child-slug]') const date = await page.$text('[data-date-child-slug]')
@ -96,7 +107,7 @@ test('Navigate to /patch/2/child/1', async t => {
dates.slug = date dates.slug = date
}) })
test('Navigate to /patch/2/child/1?foo=bar', async t => { test.serial('Navigate to /patch/2/child/1?foo=bar', async t => {
await page.nuxt.navigate('/patch/2/child/1?foo=bar') await page.nuxt.navigate('/patch/2/child/1?foo=bar')
t.is(dates.patch, await page.$text('[data-date-patch]')) t.is(dates.patch, await page.$text('[data-date-patch]'))
@ -105,7 +116,7 @@ test('Navigate to /patch/2/child/1?foo=bar', async t => {
t.is(dates.slug, await page.$text('[data-date-child-slug]')) t.is(dates.slug, await page.$text('[data-date-child-slug]'))
}) })
test('Search a country', async t => { test.serial('Search a country', async t => {
const countries = await page.$$text('[data-test-search-result]') const countries = await page.$$text('[data-test-search-result]')
t.is(countries.length, 5) t.is(countries.length, 5)
@ -125,10 +136,11 @@ test('Search a country', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })
test.after('Stop browser', async t => {
test.after.always('Stop browser', async t => {
await page.close() await page.close()
await browser.stop() await browser.stop()
}) })

View File

@ -1,22 +1,31 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { interceptLog } from './helpers/console'
const port = 4004 const port = 4013
// const url = (route) => 'http://localhost:' + port + route // const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/children'), rootDir: resolve(__dirname, 'fixtures/children'),
dev: false dev: false,
build: {
stats: false
}
} }
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test('/parent', async t => { test('/parent', async t => {
@ -54,6 +63,6 @@ test('/parent/validate-child?key=12345', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -11,7 +11,7 @@ const rootDir = resolve(__dirname, 'fixtures/basic')
const port = 4011 const port = 4011
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
test('bin/nuxt-build', async t => { test.serial('bin/nuxt-build', async t => {
const binBuild = resolve(__dirname, '..', 'bin', 'nuxt-build') const binBuild = resolve(__dirname, '..', 'bin', 'nuxt-build')
const { stdout, stderr } = await execify(`node ${binBuild} ${rootDir}`) const { stdout, stderr } = await execify(`node ${binBuild} ${rootDir}`)
@ -20,7 +20,7 @@ test('bin/nuxt-build', async t => {
t.true(stderr.includes('Building done')) t.true(stderr.includes('Building done'))
}) })
test('bin/nuxt-start', async t => { test.serial('bin/nuxt-start', async t => {
const binStart = resolve(__dirname, '..', 'bin', 'nuxt-start') const binStart = resolve(__dirname, '..', 'bin', 'nuxt-start')
let stdout = '' let stdout = ''
@ -79,7 +79,7 @@ test('bin/nuxt-start', async t => {
t.is(exitCode, null) t.is(exitCode, null)
}) })
test('bin/nuxt-generate', async t => { test.serial('bin/nuxt-generate', async t => {
const binGenerate = resolve(__dirname, '..', 'bin', 'nuxt-generate') const binGenerate = resolve(__dirname, '..', 'bin', 'nuxt-generate')
const { stdout, stderr } = await execify(`node ${binGenerate} ${rootDir}`) const { stdout, stderr } = await execify(`node ${binGenerate} ${rootDir}`)

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { interceptLog, interceptError, release } from './helpers/console'
const port = 4009 const port = 4009
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -9,60 +10,87 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/debug') const rootDir = resolve(__dirname, 'fixtures/debug')
let config = require(resolve(rootDir, 'nuxt.config.js')) let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir config.rootDir = rootDir
config.dev = true // Needed for _open middleware
nuxt = new Nuxt(config)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(config)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test('/test/_open (open-in-editor)', async t => { test.serial('/test/_open (open-in-editor)', async t => {
const logSpy = await interceptLog()
const { body } = await rp(url('/test/_open?file=pages/index.vue'), { resolveWithFullResponse: true }) const { body } = await rp(url('/test/_open?file=pages/index.vue'), { resolveWithFullResponse: true })
t.is(body, 'opened in editor!') t.is(body, 'opened in editor!')
release()
t.is(logSpy.getCall(0).args[0], '[open in editor]')
t.true(logSpy.calledOnce)
}) })
test('/test/_open should return error (open-in-editor)', async t => { test.serial('/test/_open should return error (open-in-editor)', async t => {
const logSpy = await interceptLog()
const { body } = await rp(url('/test/_open?file='), { resolveWithFullResponse: true }) const { body } = await rp(url('/test/_open?file='), { resolveWithFullResponse: true })
t.is(body, 'File is not specified') t.is(body, 'File is not specified')
release()
t.is(logSpy.getCall(0).args[0], '[open in editor]')
t.true(logSpy.calledOnce)
}) })
test('/test/error should return error stack trace (Youch)', async t => { test.serial('/test/error should return error stack trace (Youch)', async t => {
const errorSpy = await interceptError()
const { response, error } = await t.throws(nuxt.renderAndGetWindow(url('/test/error'))) const { response, error } = await t.throws(nuxt.renderAndGetWindow(url('/test/error')))
t.is(response.statusCode, 500) t.is(response.statusCode, 500)
t.is(response.statusMessage, 'NuxtServerError') t.is(response.statusMessage, 'NuxtServerError')
t.true(error.includes('test youch !')) t.true(error.includes('test youch !'))
t.true(error.includes('<div class="error-frames">')) t.true(error.includes('<div class="error-frames">'))
release()
t.true(errorSpy.calledTwice)
t.true(errorSpy.getCall(0).args[0].includes('test youch !'))
t.true(errorSpy.getCall(1).args[0].message.includes('test youch !'))
}) })
test('/test/error no source-map (Youch)', async t => { test.serial('/test/error no source-map (Youch)', async t => {
const sourceMaps = nuxt.renderer.resources.serverBundle.maps const sourceMaps = nuxt.renderer.resources.serverBundle.maps
nuxt.renderer.resources.serverBundle.maps = {} nuxt.renderer.resources.serverBundle.maps = {}
const errorSpy = await interceptError()
const { response, error } = await t.throws(nuxt.renderAndGetWindow(url('/test/error'))) const { response, error } = await t.throws(nuxt.renderAndGetWindow(url('/test/error')))
t.is(response.statusCode, 500) t.is(response.statusCode, 500)
t.is(response.statusMessage, 'NuxtServerError') t.is(response.statusMessage, 'NuxtServerError')
t.true(error.includes('test youch !')) t.true(error.includes('test youch !'))
t.true(error.includes('<div class="error-frames">')) t.true(error.includes('<div class="error-frames">'))
release()
t.true(errorSpy.calledTwice)
t.true(errorSpy.getCall(0).args[0].includes('test youch !'))
t.true(errorSpy.getCall(1).args[0].message.includes('test youch !'))
nuxt.renderer.resources.serverBundle.maps = sourceMaps nuxt.renderer.resources.serverBundle.maps = sourceMaps
}) })
test('/test/error should return json format error (Youch)', async t => { test.serial('/test/error should return json format error (Youch)', async t => {
const opts = { const opts = {
headers: { headers: {
'accept': 'application/json' 'accept': 'application/json'
}, },
resolveWithFullResponse: true resolveWithFullResponse: true
} }
const errorSpy = await interceptError()
const { response: { headers } } = await t.throws(rp(url('/test/error'), opts)) const { response: { headers } } = await t.throws(rp(url('/test/error'), opts))
t.is(headers['content-type'], 'text/json; charset=utf-8') t.is(headers['content-type'], 'text/json; charset=utf-8')
release()
t.true(errorSpy.calledTwice)
t.true(errorSpy.getCall(0).args[0].includes('test youch !'))
t.true(errorSpy.getCall(1).args[0].message.includes('test youch !'))
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -1,50 +1,52 @@
import test from 'ava' import test from 'ava'
import { interceptWarn, release } from './helpers/console'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { intercept, interceptWarn, release } from './helpers/console'
const port = 4010 const port = 4010
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null let builder = null
let buildLog = null let buildSpies = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/deprecate') const rootDir = resolve(__dirname, 'fixtures/deprecate')
let config = require(resolve(rootDir, 'nuxt.config.js')) let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir config.rootDir = rootDir
config.dev = false config.dev = false
nuxt = new Nuxt(config)
builder = new Builder(nuxt)
buildLog = await interceptWarn() buildSpies = await intercept(async () => {
await builder.build() nuxt = new Nuxt(config)
release() builder = await new Builder(nuxt)
await builder.build()
await nuxt.listen(port, 'localhost')
})
await nuxt.listen(port, 'localhost') t.true(buildSpies.log.calledWithMatch('DONE'))
t.true(buildSpies.log.calledWithMatch('OPEN'))
}) })
test('Deprecated: context.isServer and context.isClient', async t => { test.serial('Deprecated: context.isServer and context.isClient', async t => {
const logSpy = await interceptWarn() const warnSpy = await interceptWarn()
await rp(url('/')) await rp(url('/'))
t.true(logSpy.calledWith('context.isServer has been deprecated, please use process.server instead.')) t.true(warnSpy.calledWith('context.isServer has been deprecated, please use process.server instead.'))
t.true(logSpy.calledWith('context.isClient has been deprecated, please use process.client instead.')) t.true(warnSpy.calledWith('context.isClient has been deprecated, please use process.client instead.'))
t.true(logSpy.calledTwice) t.true(warnSpy.calledTwice)
release() release()
}) })
test('Deprecated: dev in build.extend()', async t => { test.serial('Deprecated: dev in build.extend()', async t => {
t.true(buildLog.withArgs('dev has been deprecated in build.extend(), please use isDev').calledTwice) t.true(buildSpies.warn.withArgs('dev has been deprecated in build.extend(), please use isDev').calledTwice)
}) })
test('Deprecated: nuxt.plugin()', async t => { test.serial('Deprecated: nuxt.plugin()', async t => {
t.true(nuxt.__builder_plugin) t.true(nuxt.__builder_plugin)
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -6,7 +6,7 @@ import { Nuxt, Builder } from '..'
import { interceptLog, release } from './helpers/console' import { interceptLog, release } from './helpers/console'
const readFile = promisify(fs.readFile) const readFile = promisify(fs.readFile)
const rootDir = resolve(__dirname, './fixtures/dll') const rootDir = resolve(__dirname, 'fixtures/dll')
const dllDir = resolve(rootDir, '.cache/client-dll') const dllDir = resolve(rootDir, '.cache/client-dll')
const checkCache = (lib) => { const checkCache = (lib) => {
@ -19,12 +19,16 @@ const checkCache = (lib) => {
let nuxt let nuxt
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js')) let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir config.rootDir = rootDir
config.dev = true config.dev = true
nuxt = new Nuxt(config)
await new Builder(nuxt).build() const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(config)
await new Builder(nuxt).build()
})
t.true(logSpy.calledWithMatch('DONE'))
}) })
test('Check vue cache', checkCache('vue')) test('Check vue cache', checkCache('vue'))
@ -41,6 +45,6 @@ test('Build with DllReferencePlugin', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing nuxt.js', t => { test.after.always('Closing nuxt.js', t => {
nuxt.close() nuxt.close()
}) })

View File

@ -3,15 +3,24 @@ import { resolve } from 'path'
import fs from 'fs' import fs from 'fs'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { promisify } from 'util' import { promisify } from 'util'
import { interceptLog } from './helpers/console'
const readFile = promisify(fs.readFile) const readFile = promisify(fs.readFile)
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const nuxt = new Nuxt({ const config = {
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'), rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
dev: false dev: false,
build: {
stats: false
}
}
const logSpy = await interceptLog(async () => {
const nuxt = new Nuxt(config)
await new Builder(nuxt).build()
}) })
await new Builder(nuxt).build() t.true(logSpy.calledWithMatch('DONE'))
}) })
test('Check .nuxt/router.js', t => { test('Check .nuxt/router.js', t => {

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { interceptLog, interceptError, release } from './helpers/console'
const port = 4005 const port = 4005
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -9,48 +10,61 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const options = {
rootDir: resolve(__dirname, 'fixtures/error'), rootDir: resolve(__dirname, 'fixtures/error'),
dev: false dev: false,
build: {
stats: false
}
} }
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test('/ should display an error', async t => { test.serial('/ should display an error', async t => {
try { const error = await t.throws(nuxt.renderRoute('/'))
await nuxt.renderRoute('/') t.true(error.message.includes('not_defined is not defined'))
} catch (e) {
t.true(e.message.includes('not_defined is not defined'))
}
}) })
test('/404 should display an error too', async t => { test.serial('/404 should display an error too', async t => {
let { error } = await nuxt.renderRoute('/404') let { error } = await nuxt.renderRoute('/404')
t.true(error.message.includes('This page could not be found')) t.true(error.message.includes('This page could not be found'))
}) })
test('/ with renderAndGetWindow()', async t => { test.serial('/ with renderAndGetWindow()', async t => {
const errorSpy = await interceptError()
const err = await t.throws(nuxt.renderAndGetWindow(url('/'))) const err = await t.throws(nuxt.renderAndGetWindow(url('/')))
t.is(err.response.statusCode, 500) t.is(err.response.statusCode, 500)
t.is(err.response.statusMessage, 'NuxtServerError') t.is(err.response.statusMessage, 'NuxtServerError')
release()
t.true(errorSpy.calledOnce)
t.true(errorSpy.getCall(0).args[0].message.includes('render function or template not defined in component: anonymous'))
}) })
test('/ with text/json content', async t => { test.serial('/ with text/json content', async t => {
const opts = { const opts = {
headers: { headers: {
'accept': 'application/json' 'accept': 'application/json'
}, },
resolveWithFullResponse: true resolveWithFullResponse: true
} }
const errorSpy = await interceptError()
const { response: { headers } } = await t.throws(rp(url('/'), opts)) const { response: { headers } } = await t.throws(rp(url('/'), opts))
t.is(headers['content-type'], 'text/json; charset=utf-8') t.is(headers['content-type'], 'text/json; charset=utf-8')
release()
t.true(errorSpy.calledOnce)
t.true(errorSpy.getCall(0).args[0].message.includes('render function or template not defined in component: anonymous'))
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -3,6 +3,7 @@ import { resolve } from 'path'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import express from 'express' import express from 'express'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { interceptLog } from './helpers/console'
const port = 4000 const port = 4000
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -11,17 +12,21 @@ let nuxt
let app let app
// Init nuxt.js and create express server // Init nuxt.js and create express server
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const config = {
rootDir: resolve(__dirname, 'fixtures/basic'), rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false buildDir: '.nuxt-express',
dev: false,
build: {
stats: false
}
} }
// Create nuxt instace const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options) nuxt = new Nuxt(config)
await new Builder(nuxt).build()
// Build })
await new Builder(nuxt).build() t.true(logSpy.calledWithMatch('DONE'))
// Create express app // Create express app
app = express() app = express()

View File

@ -2,9 +2,13 @@ module.exports = {
router: { router: {
base: '/test/' base: '/test/'
}, },
dev: true, // Needed for _open middleware
debug: true, debug: true,
editor: { editor: {
cmd: 'echo', cmd: 'echo',
pattern: '' pattern: ''
},
build: {
stats: false
} }
} }

View File

@ -3,6 +3,7 @@ module.exports = {
'~/modules/hooks' '~/modules/hooks'
], ],
build: { build: {
stats: false,
extend(config, options) { extend(config, options) {
if (options.dev) { if (options.dev) {
// Please use isDev instead of dev // Please use isDev instead of dev

View File

@ -1,5 +1,6 @@
module.exports = { module.exports = {
build: { build: {
stats: false,
dll: true, dll: true,
extend(config, options) { extend(config, options) {
if (options.isClient) { if (options.isClient) {

View File

@ -28,5 +28,8 @@ module.exports = {
handler: '~/modules/middleware/use-middleware' handler: '~/modules/middleware/use-middleware'
}) })
}) })
},
build: {
stats: false
} }
} }

View File

@ -1,6 +1,8 @@
module.exports = { module.exports = {
rootDir: __dirname,
mode: 'spa', mode: 'spa',
dev: false, dev: false,
transition: false transition: false,
build: {
stats: false
}
} }

10
test/fixtures/ssr/nuxt.config.js vendored Normal file
View File

@ -0,0 +1,10 @@
module.exports = {
dev: false,
render: {
resourceHints: false
},
build: {
stats: false,
extractCSS: true
}
}

View File

@ -42,6 +42,7 @@ module.exports = {
} }
}, },
build: { build: {
stats: false,
publicPath: '/orion/', publicPath: '/orion/',
analyze: { analyze: {
analyzerMode: 'disabled', analyzerMode: 'disabled',

View File

@ -20,6 +20,12 @@ export function release() {
if (context.error) { if (context.error) {
console.error = context.error // eslint-disable-line no-console console.error = context.error // eslint-disable-line no-console
} }
if (context.stdout) {
process.stdout.write = context.stdout
}
if (context.stderr) {
process.stderr.write = context.stderr
}
context = null context = null
} }
@ -68,18 +74,32 @@ export async function intercept(levels, msg, cb) {
spies.error = console.error = sinon.spy() // eslint-disable-line no-console spies.error = console.error = sinon.spy() // eslint-disable-line no-console
} }
if (levels && levels.stdout) {
context.stdout = process.stdout.write
spies.stdout = process.stdout.write = sinon.spy()
}
if (levels && levels.stderr) {
context.stdout = process.stderr.write
spies.stdout = process.stderr.write = sinon.spy()
}
if (cb) { if (cb) {
if (msg) { if (msg) {
process.stdout.write(` ${msg}`) if (context.stdout) {
context.stdout(` ${msg}`)
} else {
process.stdout.write(` ${msg}`)
}
} }
await cb() await cb()
release()
if (msg) { if (msg) {
process.stdout.write('\n') process.stdout.write('\n')
} }
release()
} }
return spies return spies
@ -104,3 +124,13 @@ export async function interceptError(msg, cb) {
const { error } = await intercept({ error: true }, msg, cb) const { error } = await intercept({ error: true }, msg, cb)
return error return error
} }
export async function interceptStdout(msg, cb) {
const { stderr } = await intercept({ stdout: true }, msg, cb)
return stderr
}
export async function interceptStderr(msg, cb) {
const { stderr } = await intercept({ stderr: true }, msg, cb)
return stderr
}

View File

@ -6,7 +6,7 @@ test('Nuxt.js Class', t => {
t.is(typeof Nuxt, 'function') t.is(typeof Nuxt, 'function')
}) })
test.serial('Nuxt.js Instance', async t => { test('Nuxt.js Instance', async t => {
const nuxt = new Nuxt({ const nuxt = new Nuxt({
rootDir: resolve(__dirname, 'fixtures', 'empty') rootDir: resolve(__dirname, 'fixtures', 'empty')
}) })
@ -17,7 +17,7 @@ test.serial('Nuxt.js Instance', async t => {
t.is(nuxt.initialized, true) t.is(nuxt.initialized, true)
}) })
test.serial('Fail to build when no pages/ directory but is in the parent', t => { test('Fail to build when no pages/ directory but is in the parent', t => {
const nuxt = new Nuxt({ const nuxt = new Nuxt({
dev: false, dev: false,
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages') rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
@ -29,7 +29,7 @@ test.serial('Fail to build when no pages/ directory but is in the parent', t =>
}) })
}) })
test.serial('Fail to build when no pages/ directory', t => { test('Fail to build when no pages/ directory', t => {
const nuxt = new Nuxt({ const nuxt = new Nuxt({
dev: false, dev: false,
rootDir: resolve(__dirname) rootDir: resolve(__dirname)

View File

@ -2,60 +2,62 @@ import test from 'ava'
import { resolve, normalize } from 'path' import { resolve, normalize } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { interceptError, release } from './helpers/console' import { intercept } from './helpers/console'
const port = 4006 const port = 4006
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null let builder = null
let builtErr = null let buildSpies = null
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/module') const rootDir = resolve(__dirname, 'fixtures/module')
let config = require(resolve(rootDir, 'nuxt.config.js')) const config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir config.rootDir = rootDir
config.dev = false config.dev = false
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
builder = new Builder(nuxt) builder = new Builder(nuxt)
builtErr = await interceptError() buildSpies = await intercept({ log: true, error: true }, async () => {
await builder.build() await builder.build()
release() await nuxt.listen(port, 'localhost')
})
await nuxt.listen(port, 'localhost') t.true(buildSpies.log.calledWithMatch('DONE'))
t.true(buildSpies.log.calledWithMatch('OPEN'))
}) })
test('Vendor', async t => { test.serial('Vendor', async t => {
t.true(nuxt.options.build.vendor.indexOf('lodash') !== -1, 'lodash added to config') t.true(nuxt.options.build.vendor.indexOf('lodash') !== -1, 'lodash added to config')
}) })
test('Plugin', async t => { test.serial('Plugin', async t => {
t.true(normalize(nuxt.options.plugins[0].src) t.true(normalize(nuxt.options.plugins[0].src)
.includes(normalize('fixtures/module/.nuxt/basic.reverse.')), 'plugin added to config') .includes(normalize('fixtures/module/.nuxt/basic.reverse.')), 'plugin added to config')
const { html } = await nuxt.renderRoute('/') const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>TXUN</h1>'), 'plugin works') t.true(html.includes('<h1>TXUN</h1>'), 'plugin works')
}) })
test('Middleware', async t => { test.serial('Hooks', async t => {
let response = await rp(url('/api'))
t.is(response, 'It works!', '/api response is correct')
})
test('Hooks', async t => {
t.is(nuxt.__module_hook, 1) t.is(nuxt.__module_hook, 1)
t.is(nuxt.__renderer_hook, 2) t.is(nuxt.__renderer_hook, 2)
t.is(nuxt.__builder_hook, 3) t.is(nuxt.__builder_hook, 3)
}) })
test('Hooks - Functional', async t => { test.serial('Hooks - Functional', async t => {
t.true(nuxt.__ready_called__) t.true(nuxt.__ready_called__)
t.true(builder.__build_done__) t.true(builder.__build_done__)
}) })
test('Hooks - Error', async t => { test.serial('Hooks - Error', async t => {
t.true(builtErr.calledWithMatch(/build:extendRoutes/)) t.true(buildSpies.error.calledWithMatch(/build:extendRoutes/))
})
test('Middleware', async t => {
let response = await rp(url('/api'))
t.is(response, 'It works!', '/api response is correct')
}) })
test('Hooks - Use external middleware before render', async t => { test('Hooks - Use external middleware before render', async t => {
@ -64,6 +66,6 @@ test('Hooks - Use external middleware before render', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -1,10 +1,11 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { interceptLog, release } from './helpers/console' import { interceptLog, release } from './helpers/console'
let nuxt = null let nuxt = null
const port = 4004 const port = 4012
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
const renderRoute = async _url => { const renderRoute = async _url => {
@ -15,28 +16,49 @@ const renderRoute = async _url => {
} }
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
nuxt = new Nuxt(require('./fixtures/spa/nuxt.config')) const rootDir = resolve(__dirname, 'fixtures/spa')
await new Builder(nuxt).build() const config = require(resolve(rootDir, 'nuxt.config.js'))
await nuxt.listen(port, 'localhost') config.rootDir = rootDir
const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(config)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test('/ (basic spa)', async t => { test.serial('/ (basic spa)', async t => {
const logSpy = await interceptLog()
const { html } = await renderRoute('/') const { html } = await renderRoute('/')
t.true(html.includes('Hello SPA!')) t.true(html.includes('Hello SPA!'))
release()
t.true(logSpy.withArgs('created').notCalled)
t.true(logSpy.withArgs('mounted').calledOnce)
}) })
test('/custom (custom layout)', async t => { test.serial('/custom (custom layout)', async t => {
const logSpy = await interceptLog()
const { html } = await renderRoute('/custom') const { html } = await renderRoute('/custom')
t.true(html.includes('Custom layout')) t.true(html.includes('Custom layout'))
release()
t.true(logSpy.withArgs('created').calledOnce)
t.true(logSpy.withArgs('mounted').calledOnce)
}) })
test('/custom (not default layout)', async t => { test.serial('/custom (not default layout)', async t => {
const logSpy = await interceptLog()
const { head } = await renderRoute('/custom') const { head } = await renderRoute('/custom')
t.false(head.includes('src="/_nuxt/layouts/default.')) t.false(head.includes('src="/_nuxt/layouts/default.'))
release()
t.true(logSpy.withArgs('created').calledOnce)
t.true(logSpy.withArgs('mounted').calledOnce)
}) })
test('/custom (call mounted and created once)', async t => { test.serial('/custom (call mounted and created once)', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
await renderRoute('/custom') await renderRoute('/custom')
release() release()
@ -51,6 +73,6 @@ test('/_nuxt/ (access publicPath in spa mode)', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -3,6 +3,7 @@ import { resolve } from 'path'
import { Nuxt, Builder, Utils } from '..' import { Nuxt, Builder, Utils } from '..'
import { uniq } from 'lodash' import { uniq } from 'lodash'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { interceptLog } from './helpers/console'
const port = 4008 const port = 4008
let nuxt = null let nuxt = null
@ -17,20 +18,19 @@ const url = (route) => 'http://localhost:' + port + route
const isWindows = /^win/.test(process.platform) const isWindows = /^win/.test(process.platform)
// Init nuxt.js and create server listening on localhost:4000 // Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => { test.serial('Init Nuxt.js', async t => {
const options = { const rootDir = resolve(__dirname, 'fixtures/ssr')
rootDir: resolve(__dirname, 'fixtures/ssr'), const config = require(resolve(rootDir, 'nuxt.config.js'))
dev: false, config.rootDir = rootDir
render: {
resourceHints: false const logSpy = await interceptLog(async () => {
}, nuxt = new Nuxt(config)
build: { await new Builder(nuxt).build()
extractCSS: true await nuxt.listen(port, 'localhost')
} })
}
nuxt = new Nuxt(options) t.true(logSpy.calledWithMatch('DONE'))
await new Builder(nuxt).build() t.true(logSpy.calledWithMatch('OPEN'))
await nuxt.listen(port, 'localhost')
}) })
// == Uniq Test == // == Uniq Test ==
@ -121,6 +121,6 @@ test('stress test with asyncData', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after.always('Closing server and nuxt.js', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -16,19 +16,23 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js')) let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir config.rootDir = rootDir
config.dev = false config.dev = false
nuxt = new Nuxt(config)
builder = new Builder(nuxt) const logSpy = await interceptLog(async () => {
await interceptLog('building nuxt', async () => { nuxt = new Nuxt(config)
builder = new Builder(nuxt)
await builder.build() await builder.build()
await nuxt.listen(port, 'localhost')
}) })
await nuxt.listen(port, 'localhost')
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
}) })
test('/', async t => { test.serial('/', async t => {
const logSpy = await interceptLog(async () => { const logSpy = await interceptLog()
const { html } = await nuxt.renderRoute('/') const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom configurations</h1>')) t.true(html.includes('<h1>I have custom configurations</h1>'))
}) release()
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!') t.is(logSpy.args[0][0], 'Test plugin!')
}) })
@ -58,21 +62,20 @@ test('/ (custom postcss.config.js)', async t => {
t.true(html.includes('::-webkit-input-placeholder')) t.true(html.includes('::-webkit-input-placeholder'))
}) })
test('/test/ (router base)', async t => { test.serial('/test/ (router base)', async t => {
const logSpy = await interceptLog(async () => { const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/')) const window = await nuxt.renderAndGetWindow(url('/test/'))
const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'default')
t.true(html.includes('<h1>Default layout</h1>'))
t.true(html.includes('<h1>I have custom configurations</h1>'))
})
const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'default')
t.true(html.includes('<h1>Default layout</h1>'))
t.true(html.includes('<h1>I have custom configurations</h1>'))
release()
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!') t.is(logSpy.args[0][0], 'Test plugin!')
}) })
test('/test/about (custom layout)', async t => { test.serial('/test/about (custom layout)', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about')) const window = await nuxt.renderAndGetWindow(url('/test/about'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -85,7 +88,7 @@ test('/test/about (custom layout)', async t => {
t.true(html.includes('<h1>About page</h1>')) t.true(html.includes('<h1>About page</h1>'))
}) })
test('/test/desktop (custom layout in desktop folder)', async t => { test.serial('/test/desktop (custom layout in desktop folder)', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/desktop')) const window = await nuxt.renderAndGetWindow(url('/test/desktop'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -98,7 +101,7 @@ test('/test/desktop (custom layout in desktop folder)', async t => {
t.true(html.includes('<h1>Desktop page</h1>')) t.true(html.includes('<h1>Desktop page</h1>'))
}) })
test('/test/mobile (custom layout in mobile folder)', async t => { test.serial('/test/mobile (custom layout in mobile folder)', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/mobile')) const window = await nuxt.renderAndGetWindow(url('/test/mobile'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -111,7 +114,7 @@ test('/test/mobile (custom layout in mobile folder)', async t => {
t.true(html.includes('<h1>Mobile page</h1>')) t.true(html.includes('<h1>Mobile page</h1>'))
}) })
test('/test/env', async t => { test.serial('/test/env', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/env')) const window = await nuxt.renderAndGetWindow(url('/test/env'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -129,7 +132,7 @@ test('/test/env', async t => {
t.true(html.includes('"obj": {')) t.true(html.includes('"obj": {'))
}) })
test('/test/error', async t => { test.serial('/test/error', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/error')) const window = await nuxt.renderAndGetWindow(url('/test/error'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -140,7 +143,7 @@ test('/test/error', async t => {
t.true(html.includes('Error page')) t.true(html.includes('Error page'))
}) })
test('/test/user-agent', async t => { test.serial('/test/user-agent', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/user-agent')) const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -151,7 +154,7 @@ test('/test/user-agent', async t => {
t.true(html.includes('<pre>Mozilla')) t.true(html.includes('<pre>Mozilla'))
}) })
test('/test/about-bis (added with extendRoutes)', async t => { test.serial('/test/about-bis (added with extendRoutes)', async t => {
const logSpy = await interceptLog() const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about-bis')) const window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
t.true(logSpy.calledOnce) t.true(logSpy.calledOnce)
@ -190,6 +193,6 @@ test('Check build.styleResources for style-resources-loader', async t => {
}) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', async t => { test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close() await nuxt.close()
}) })