test: coverage for hooks

This commit is contained in:
Clark Du 2017-11-19 22:35:11 +08:00 committed by Pooya Parsa
parent 377416a7ef
commit f0ea07ea4b
3 changed files with 28 additions and 3 deletions

View File

@ -15,4 +15,9 @@ module.exports = function () {
this.nuxt.hook('build:done', (builder) => { this.nuxt.hook('build:done', (builder) => {
this.nuxt.__builder_hook = builder && ctr++ this.nuxt.__builder_hook = builder && ctr++
}) })
// Note: Plugin is deprecated. Please use new hooks system.
this.nuxt.plugin('built', (builder) => {
this.nuxt.__builder_plugin = builder && ctr++
})
} }

View File

@ -1,7 +1,7 @@
module.exports = { module.exports = {
loading: true, loading: true,
modules: [ modules: [
'~/modules/basic', '~~/modules/basic',
'~/modules/hooks', '~/modules/hooks',
{ {
src: '~/modules/middleware', src: '~/modules/middleware',
@ -13,5 +13,13 @@ module.exports = {
], ],
serverMiddleware: [ serverMiddleware: [
'./modules/middleware/midd2' './modules/middleware/midd2'
] ],
hooks(hook) {
hook('ready', nuxt => {
nuxt.__ready_called__ = true
})
hook('build:done', builder => {
builder.__build_done__ = true
})
}
} }

View File

@ -7,6 +7,7 @@ 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
// 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.before('Init Nuxt.js', async t => {
@ -15,7 +16,8 @@ test.before('Init Nuxt.js', async t => {
config.rootDir = rootDir config.rootDir = rootDir
config.dev = false config.dev = false
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
await new Builder(nuxt).build() builder = new Builder(nuxt)
await builder.build()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}) })
@ -42,6 +44,16 @@ test('Hooks', async t => {
t.is(nuxt.__builder_hook, 3) t.is(nuxt.__builder_hook, 3)
}) })
test('Hooks - Functional', async t => {
t.true(nuxt.__ready_called__)
t.true(builder.__build_done__)
})
// Note: Plugin is deprecated. Please use new hooks system.
test('Plugin', async t => {
t.is(nuxt.__builder_plugin, 4)
})
// 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('Closing server and nuxt.js', t => {
nuxt.close() nuxt.close()