From f0ea07ea4be9d589b1269646830c9ebb5a5795c8 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Sun, 19 Nov 2017 22:35:11 +0800 Subject: [PATCH] test: coverage for hooks --- test/fixtures/module/modules/hooks/index.js | 5 +++++ test/fixtures/module/nuxt.config.js | 12 ++++++++++-- test/module.test.js | 14 +++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/test/fixtures/module/modules/hooks/index.js b/test/fixtures/module/modules/hooks/index.js index 5349106c6e..02722eacc1 100644 --- a/test/fixtures/module/modules/hooks/index.js +++ b/test/fixtures/module/modules/hooks/index.js @@ -15,4 +15,9 @@ module.exports = function () { this.nuxt.hook('build:done', (builder) => { 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++ + }) } diff --git a/test/fixtures/module/nuxt.config.js b/test/fixtures/module/nuxt.config.js index d60660981f..63b3a2f62a 100755 --- a/test/fixtures/module/nuxt.config.js +++ b/test/fixtures/module/nuxt.config.js @@ -1,7 +1,7 @@ module.exports = { loading: true, modules: [ - '~/modules/basic', + '~~/modules/basic', '~/modules/hooks', { src: '~/modules/middleware', @@ -13,5 +13,13 @@ module.exports = { ], serverMiddleware: [ './modules/middleware/midd2' - ] + ], + hooks(hook) { + hook('ready', nuxt => { + nuxt.__ready_called__ = true + }) + hook('build:done', builder => { + builder.__build_done__ = true + }) + } } diff --git a/test/module.test.js b/test/module.test.js index a7e6c8eeb1..739ae97363 100755 --- a/test/module.test.js +++ b/test/module.test.js @@ -7,6 +7,7 @@ const port = 4006 const url = (route) => 'http://localhost:' + port + route let nuxt = null +let builder = null // Init nuxt.js and create server listening on localhost:4000 test.before('Init Nuxt.js', async t => { @@ -15,7 +16,8 @@ test.before('Init Nuxt.js', async t => { config.rootDir = rootDir config.dev = false nuxt = new Nuxt(config) - await new Builder(nuxt).build() + builder = new Builder(nuxt) + await builder.build() await nuxt.listen(port, 'localhost') }) @@ -42,6 +44,16 @@ test('Hooks', async t => { 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 test.after('Closing server and nuxt.js', t => { nuxt.close()