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*
# Other
.nuxt
.nuxt*
.cache
# Dist folder

View File

@ -45,7 +45,7 @@
]
},
"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",
"lint": "eslint --ext .js,.vue bin/* build/ lib/ test/ examples/",
"precommit": "npm run lint",

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder } from '..'
import * as browser from './helpers/browser'
import { interceptLog } from './helpers/console'
const port = 4003
const url = (route) => 'http://localhost:' + port + route
@ -10,36 +11,46 @@ let nuxt = null
let page = null
// 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 = {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false,
buildDir: '.nuxt-csr',
dev: true,
head: {
titleTemplate(titleChunk) {
return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js'
}
},
build: {
stats: false
}
}
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({
// slowMo: 50,
// headless: false
})
})
test('Open /', async t => {
test.serial('Open /', async t => {
page = await browser.page(url('/'))
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 loading = await page.nuxt.loadingData()
@ -48,27 +59,27 @@ test('/stateless', async t => {
t.is(await page.$text('h1'), 'My component!')
})
test('/css', async t => {
test.serial('/css', async t => {
await page.nuxt.navigate('/css')
t.is(await page.$text('.red'), 'This is red')
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')
t.is(await page.$text('p'), 'The answer is 42')
})
test('/store', async t => {
test.serial('/store', async t => {
await page.nuxt.navigate('/store')
t.is(await page.$text('h1'), 'Vuex Nested Modules')
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)))
await page.nuxt.navigate('/head')
const metas = await page.$$attr('meta', 'content')
@ -79,31 +90,31 @@ test('/head', async t => {
t.is(metas[0], 'my meta')
})
test('/async-data', async t => {
test.serial('/async-data', async t => {
await page.nuxt.navigate('/async-data')
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')
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')
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')
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')
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')
})
test('/validate?valid=true', async t => {
test.serial('/validate?valid=true', async t => {
await page.nuxt.navigate('/validate?valid=true')
t.is(await page.$text('h1'), 'I am valid')
})
test('/redirect', async t => {
test.serial('/redirect', async t => {
await page.nuxt.navigate('/redirect')
t.is(await page.$text('h1'), 'Index page')
})
test('/error', async t => {
test.serial('/error', async t => {
await page.nuxt.navigate('/error')
t.deepEqual(await page.nuxt.errorData(), { statusCode: 500 })
t.is(await page.$text('.title'), 'Error mouahahah')
})
test('/error2', async t => {
test.serial('/error2', async t => {
await page.nuxt.navigate('/error2')
t.is(await page.$text('.title'), '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')
t.is(await page.$text('h1'), 'Index page')
})
test('/redirect3', async t => {
test.serial('/redirect3', async t => {
// New page for redirecting to external link.
const page = await browser.page(url('/'))
await page.nuxt.navigate('/redirect3', false)
@ -152,44 +163,44 @@ test('/redirect3', async t => {
t.pass()
})
test('/no-ssr', async t => {
test.serial('/no-ssr', async t => {
await page.nuxt.navigate('/no-ssr')
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')
const state = await page.nuxt.storeState()
t.deepEqual(state.meta, [{ works: true }])
})
test('/fn-midd', async t => {
test.serial('/fn-midd', async t => {
await page.nuxt.navigate('/fn-midd')
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 })
})
test('/fn-midd?please=true', async t => {
test.serial('/fn-midd?please=true', async t => {
await page.nuxt.navigate('/fn-midd?please=true')
const h1 = await page.$text('h1')
t.true(h1.includes('Date:'))
})
test('/router-guard', async t => {
test.serial('/router-guard', async t => {
await page.nuxt.navigate('/router-guard')
t.is(await page.$text('p'), 'Nuxt.js')
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})
test.after('Stop browser', async t => {
test.after.always('Stop browser', async t => {
await browser.stop()
})

View File

@ -1,6 +1,6 @@
import test from 'ava'
import { resolve } from 'path'
import { interceptLog, release } from './helpers/console'
import { intercept, release } from './helpers/console'
import { Nuxt, Builder, Utils } from '..'
import { truncateSync, readFileSync, writeFileSync } from 'fs'
@ -13,32 +13,39 @@ const pluginContent = readFileSync(pluginPath)
let nuxt = null
// 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 = {
rootDir,
buildDir: '.nuxt-dev',
dev: true,
build: {
stats: false,
profile: true
},
plugins: [
'~/plugins/watch.js'
]
}
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 => {
const logSpy = await interceptLog()
test.serial('remove mixins in live reloading', async t => {
const spies = await intercept()
await nuxt.renderRoute(url('/'))
t.true(logSpy.calledWith('I am mixin'))
t.true(spies.log.calledWith('I am mixin'))
truncateSync(pluginPath)
await new Promise(async (resolve, reject) => {
let waitTimes = 0
while (logSpy.neverCalledWithMatch(/Compiled successfully/)) {
while (spies.log.neverCalledWithMatch(/Compiled successfully/)) {
if (waitTimes++ >= 20) {
t.fail('Dev server doesn\'t reload after 2000ms')
reject(Error())
@ -47,16 +54,21 @@ test('remove mixins in live reloading', async t => {
}
resolve()
})
logSpy.reset()
spies.log.reset()
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()
})
test('/stateless', async t => {
test.serial('/stateless', async t => {
const spies = await intercept()
const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML
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 => {
@ -69,7 +81,7 @@ test('/stateless', async t => {
// })
// 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)
await nuxt.close()
})

View File

@ -1,22 +1,32 @@
import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder, Generator } from '..'
import { intercept } from './helpers/console'
test('Fail with routes() which throw an error', async t => {
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),
buildDir: '.nuxt-fail',
dev: false,
build: {
stats: false
},
generate: {
async routes() {
throw new Error('Not today!')
}
}
}
const spies = await intercept(async () => {
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
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 { resolve } from 'path'
import { existsSync } from 'fs'
import { remove } from 'fs-extra'
import http from 'http'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import rp from 'request-promise-native'
import { interceptLog, release } from './helpers/console'
import { Nuxt, Builder, Generator } from '..'
const port = 4002
const url = (route) => 'http://localhost:' + port + route
const rootDir = resolve(__dirname, 'fixtures/basic')
let nuxt = null
let server = null
let generator = null
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/basic')
test.serial('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.buildDir = '.nuxt-generate'
config.dev = false
config.build.stats = false
const logSpy = await interceptLog(async () => {
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) {
}
generator = new Generator(nuxt, builder)
await generator.generate()
})
t.true(logSpy.calledWithMatch('DONE'))
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
@ -33,17 +41,17 @@ test.before('Init Nuxt.js', async t => {
server.listen(port)
})
test('Check ready hook called', async t => {
test.serial('Check ready hook called', async t => {
t.true(nuxt.__hook_called__)
})
test('/stateless', async t => {
test.serial('/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 => {
test.serial('/css', async t => {
const window = await nuxt.renderAndGetWindow(url('/css'))
const element = window.document.querySelector('.red')
t.not(element, null)
@ -52,84 +60,106 @@ test('/css', async t => {
t.is(window.getComputedStyle(element).color, 'red')
})
test('/stateful', async t => {
test.serial('/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 => {
test.serial('/head', async t => {
const logSpy = await interceptLog()
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>'))
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 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'))
test.serial('/users/1/index.html', async t => {
const html = await rp(url('/users/1/index.html'))
t.true(html.includes('<h1>User: 1</h1>'))
t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.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'))
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'))
t.true(html.includes('<h1>User: 3000</h1>'))
})
test('/users/4 -> Not found', async t => {
try {
await rp(url('/users/4'))
} catch (error) {
test.serial('/users/4 -> Not found', async t => {
const error = await t.throws(rp(url('/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'))
t.true(html.includes('<div id="__nuxt"></div>'))
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 html = window.document.body.innerHTML
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 html = window.document.body.innerHTML
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'))
t.true(html.includes('<div id="__nuxt"></div>'))
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 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()
test.serial('/users/1 not found', async t => {
await remove(resolve(rootDir, 'dist/users'))
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 rp from 'request-promise-native'
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
let nuxt = null
// 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 = {
rootDir: resolve(__dirname, 'fixtures/basic'),
buildDir: '.nuxt-ssr',
dev: false,
head: {
titleTemplate(titleChunk) {
return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js'
}
},
build: {
stats: false
}
}
await interceptLog('building nuxt', async () => {
const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
const builder = await new Builder(nuxt)
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 => {
@ -62,8 +70,8 @@ test('/store', async t => {
t.true(html.includes('<p>1</p>'))
})
test('/head', async t => {
const logSpy = await interceptLog(async () => {
test.serial('/head', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false })
t.is(window.document.title, 'My title - Nuxt.js')
@ -73,7 +81,8 @@ test('/head', async t => {
const metas = window.document.getElementsByTagName('meta')
t.is(metas[0].getAttribute('content'), 'my meta')
})
release()
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Body script!')
})
@ -145,12 +154,12 @@ test('/error', async t => {
t.true(err.message.includes('Error mouahahah'))
})
test('/error status code', async t => {
const errorSpy = await interceptError(async () => {
test.serial('/error status code', async t => {
const errorSpy = await interceptError()
const err = await t.throws(rp(url('/error')))
t.true(err.statusCode === 500)
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.args[0][0].message.includes('Error mouahahah'))
})
@ -163,29 +172,25 @@ test('/error2', async t => {
})
test('/error2 status code', async t => {
try {
await rp(url('/error2'))
} catch (err) {
t.is(err.statusCode, 500)
t.true(err.response.body.includes('Custom error'))
}
const error = await t.throws(rp(url('/error2')))
t.is(error.statusCode, 500)
t.true(error.response.body.includes('Custom error'))
})
test('/error-midd', async t => {
const errorSpy = await interceptError(async () => {
test.serial('/error-midd', async t => {
const errorSpy = await interceptError()
const err = await t.throws(rp(url('/error-midd')))
t.is(err.statusCode, 505)
t.true(err.response.body.includes('Middleware Error'))
})
release()
// Don't display error since redirect returns a noopApp
t.true(errorSpy.notCalled)
})
test('/redirect2', async t => {
const errorSpy = await interceptError(async () => {
test.serial('/redirect2', async t => {
const errorSpy = await interceptError()
await rp(url('/redirect2')) // Should not console.error
})
release()
// Don't display error since redirect returns a noopApp
t.true(errorSpy.notCalled)
})
@ -202,16 +207,12 @@ test('/no-ssr (client-side)', async t => {
})
test('ETag Header', async t => {
const errorSpy = await interceptError(async () => {
const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true })
// Validate etag
t.regex(etag, /W\/".*"$/)
// Verify functionality
const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } }))
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 => {
@ -226,7 +227,6 @@ test('/_nuxt/ should return 404', async t => {
test('/meta', async t => {
const { html } = await nuxt.renderRoute('/meta')
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
test.after('Closing server and nuxt.js', async t => {
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})

View File

@ -2,34 +2,45 @@ import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder, Utils } from '..'
import * as browser from './helpers/browser'
import { interceptLog } from './helpers/console'
const port = 4005
const port = 4014
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
let page
const dates = {}
// 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 = {
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false
buildDir: '.nuxt-patch',
dev: false,
build: {
stats: false
}
}
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({
// slowMo: 50,
// headless: false
})
})
let page
const dates = {}
test('Loading /patch and keep ', async t => {
test.serial('Loading /patch and keep ', async t => {
page = await browser.page(url('/patch'))
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]')
})
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 loading = await page.nuxt.loadingData()
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]'))
})
test('Navigate to /patch/2', async t => {
test.serial('Navigate to /patch/2', async t => {
await page.nuxt.navigate('/patch/2')
const date = await page.$text('[data-date-id]')
@ -62,19 +73,19 @@ test('Navigate to /patch/2', async t => {
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')
t.is(dates.patch, await page.$text('[data-date-patch]'))
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')
t.is(dates.patch, await page.$text('[data-date-patch]'))
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')
dates.child = await page.$text('[data-date-child]')
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)
})
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')
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
})
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')
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]'))
})
test('Search a country', async t => {
test.serial('Search a country', async t => {
const countries = await page.$$text('[data-test-search-result]')
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
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})
test.after('Stop browser', async t => {
test.after.always('Stop browser', async t => {
await page.close()
await browser.stop()
})

View File

@ -1,22 +1,31 @@
import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder } from '..'
import { interceptLog } from './helpers/console'
const port = 4004
const port = 4013
// const url = (route) => 'http://localhost:' + port + route
let nuxt = null
// 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 = {
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false
dev: false,
build: {
stats: false
}
}
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 => {
@ -54,6 +63,6 @@ test('/parent/validate-child?key=12345', async t => {
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})

View File

@ -11,7 +11,7 @@ const rootDir = resolve(__dirname, 'fixtures/basic')
const port = 4011
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 { stdout, stderr } = await execify(`node ${binBuild} ${rootDir}`)
@ -20,7 +20,7 @@ test('bin/nuxt-build', async t => {
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')
let stdout = ''
@ -79,7 +79,7 @@ test('bin/nuxt-start', async t => {
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 { stdout, stderr } = await execify(`node ${binGenerate} ${rootDir}`)

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path'
import rp from 'request-promise-native'
import { Nuxt, Builder } from '..'
import { interceptLog, interceptError, release } from './helpers/console'
const port = 4009
const url = (route) => 'http://localhost:' + port + route
@ -9,60 +10,87 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null
// 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')
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = true // Needed for _open middleware
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 })
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 })
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')))
t.is(response.statusCode, 500)
t.is(response.statusMessage, 'NuxtServerError')
t.true(error.includes('test youch !'))
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
nuxt.renderer.resources.serverBundle.maps = {}
const errorSpy = await interceptError()
const { response, error } = await t.throws(nuxt.renderAndGetWindow(url('/test/error')))
t.is(response.statusCode, 500)
t.is(response.statusMessage, 'NuxtServerError')
t.true(error.includes('test youch !'))
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
})
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 = {
headers: {
'accept': 'application/json'
},
resolveWithFullResponse: true
}
const errorSpy = await interceptError()
const { response: { headers } } = await t.throws(rp(url('/test/error'), opts))
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
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})

View File

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

View File

@ -6,7 +6,7 @@ import { Nuxt, Builder } from '..'
import { interceptLog, release } from './helpers/console'
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 checkCache = (lib) => {
@ -19,12 +19,16 @@ const checkCache = (lib) => {
let nuxt
test.before('Init Nuxt.js', async t => {
test.serial('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = true
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'))
@ -41,6 +45,6 @@ test('Build with DllReferencePlugin', async t => {
})
// 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()
})

View File

@ -3,15 +3,24 @@ import { resolve } from 'path'
import fs from 'fs'
import { Nuxt, Builder } from '..'
import { promisify } from 'util'
import { interceptLog } from './helpers/console'
const readFile = promisify(fs.readFile)
test.before('Init Nuxt.js', async t => {
const nuxt = new Nuxt({
test.serial('Init Nuxt.js', async t => {
const config = {
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()
})
t.true(logSpy.calledWithMatch('DONE'))
})
test('Check .nuxt/router.js', t => {

View File

@ -2,6 +2,7 @@ import test from 'ava'
import { resolve } from 'path'
import rp from 'request-promise-native'
import { Nuxt, Builder } from '..'
import { interceptLog, interceptError, release } from './helpers/console'
const port = 4005
const url = (route) => 'http://localhost:' + port + route
@ -9,48 +10,61 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null
// 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 = {
rootDir: resolve(__dirname, 'fixtures/error'),
dev: false
dev: false,
build: {
stats: false
}
}
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 => {
try {
await nuxt.renderRoute('/')
} catch (e) {
t.true(e.message.includes('not_defined is not defined'))
}
test.serial('/ should display an error', async t => {
const error = await t.throws(nuxt.renderRoute('/'))
t.true(error.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')
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('/')))
t.is(err.response.statusCode, 500)
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 = {
headers: {
'accept': 'application/json'
},
resolveWithFullResponse: true
}
const errorSpy = await interceptError()
const { response: { headers } } = await t.throws(rp(url('/'), opts))
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
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,8 @@
module.exports = {
rootDir: __dirname,
mode: 'spa',
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: {
stats: false,
publicPath: '/orion/',
analyze: {
analyzerMode: 'disabled',

View File

@ -20,6 +20,12 @@ export function release() {
if (context.error) {
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
}
@ -68,18 +74,32 @@ export async function intercept(levels, msg, cb) {
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 (msg) {
if (context.stdout) {
context.stdout(` ${msg}`)
} else {
process.stdout.write(` ${msg}`)
}
}
await cb()
release()
if (msg) {
process.stdout.write('\n')
}
release()
}
return spies
@ -104,3 +124,13 @@ export async function interceptError(msg, cb) {
const { error } = await intercept({ error: true }, msg, cb)
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')
})
test.serial('Nuxt.js Instance', async t => {
test('Nuxt.js Instance', async t => {
const nuxt = new Nuxt({
rootDir: resolve(__dirname, 'fixtures', 'empty')
})
@ -17,7 +17,7 @@ test.serial('Nuxt.js Instance', async t => {
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({
dev: false,
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({
dev: false,
rootDir: resolve(__dirname)

View File

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

View File

@ -1,10 +1,11 @@
import test from 'ava'
import { resolve } from 'path'
import { Nuxt, Builder } from '..'
import { interceptLog, release } from './helpers/console'
let nuxt = null
const port = 4004
const port = 4012
const url = (route) => 'http://localhost:' + port + route
const renderRoute = async _url => {
@ -15,28 +16,49 @@ const renderRoute = async _url => {
}
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
nuxt = new Nuxt(require('./fixtures/spa/nuxt.config'))
test.serial('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/spa')
const config = require(resolve(rootDir, 'nuxt.config.js'))
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('/')
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')
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')
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()
await renderRoute('/custom')
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
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})

View File

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

View File

@ -16,19 +16,23 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
const logSpy = await interceptLog(async () => {
nuxt = new Nuxt(config)
builder = new Builder(nuxt)
await interceptLog('building nuxt', async () => {
await builder.build()
})
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
})
test('/', async t => {
const logSpy = await interceptLog(async () => {
test.serial('/', async t => {
const logSpy = await interceptLog()
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom configurations</h1>'))
})
release()
t.true(logSpy.calledOnce)
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'))
})
test('/test/ (router base)', async t => {
const logSpy = await interceptLog(async () => {
test.serial('/test/ (router base)', async t => {
const logSpy = await interceptLog()
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>'))
})
release()
t.true(logSpy.calledOnce)
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 window = await nuxt.renderAndGetWindow(url('/test/about'))
t.true(logSpy.calledOnce)
@ -85,7 +88,7 @@ test('/test/about (custom layout)', async t => {
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 window = await nuxt.renderAndGetWindow(url('/test/desktop'))
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>'))
})
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 window = await nuxt.renderAndGetWindow(url('/test/mobile'))
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>'))
})
test('/test/env', async t => {
test.serial('/test/env', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/env'))
t.true(logSpy.calledOnce)
@ -129,7 +132,7 @@ test('/test/env', async t => {
t.true(html.includes('"obj": {'))
})
test('/test/error', async t => {
test.serial('/test/error', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/error'))
t.true(logSpy.calledOnce)
@ -140,7 +143,7 @@ test('/test/error', async t => {
t.true(html.includes('Error page'))
})
test('/test/user-agent', async t => {
test.serial('/test/user-agent', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
t.true(logSpy.calledOnce)
@ -151,7 +154,7 @@ test('/test/user-agent', async t => {
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 window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
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
test.after('Closing server and nuxt.js', async t => {
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
})