From 632a94a9c64fc968edc29ed004f4316547007ad8 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 15 May 2017 03:03:31 +0430 Subject: [PATCH] [module] Tests --- lib/module.js | 4 +- lib/nuxt.js | 2 +- test/fixtures/module/modules/basic/index.js | 17 ++++++++ test/fixtures/module/modules/basic/reverse.js | 11 +++++ test/fixtures/module/nuxt.config.js | 5 +++ test/fixtures/module/package.json | 5 +++ test/fixtures/module/pages/index.vue | 5 +++ test/module.test.js | 43 +++++++++++++++++++ 8 files changed, 90 insertions(+), 2 deletions(-) create mode 100755 test/fixtures/module/modules/basic/index.js create mode 100755 test/fixtures/module/modules/basic/reverse.js create mode 100755 test/fixtures/module/nuxt.config.js create mode 100755 test/fixtures/module/package.json create mode 100755 test/fixtures/module/pages/index.vue create mode 100755 test/module.test.js diff --git a/lib/module.js b/lib/module.js index ff3780184d..62b6776795 100755 --- a/lib/module.js +++ b/lib/module.js @@ -26,6 +26,7 @@ class Module { // Validate & parse source const src = template.src || template const srcPath = path.parse(src) + /* istanbul ignore if */ if (!src || typeof src !== 'string' || !fs.existsSync(src)) { // eslint-disable-next-line no-console console.warn('[Nuxt] invalid template', template) @@ -90,7 +91,7 @@ class Module { // eslint-disable-next-line no-eval module = eval('require')(src) } - } catch (e) { + } /* istanbul ignore next */ catch (e) { // eslint-disable-next-line no-console console.error('[Nuxt] Unable to resolve module', src) // eslint-disable-next-line no-console @@ -98,6 +99,7 @@ class Module { return } // Validate module + /* istanbul ignore if */ if (!(module instanceof Function)) { // eslint-disable-next-line no-console console.error('[Nuxt] Module should be a function', module) diff --git a/lib/nuxt.js b/lib/nuxt.js index 2cfc044772..a998cb80df 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -115,7 +115,7 @@ class Nuxt { // Install all modules in sequence and then return `this` instance return utils.sequence(options.modules, this.module.installModule.bind(this.module)) .then(() => this) - .catch((err) => { + .catch(/* istanbul ignore next */ (err) => { console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console console.error(err) // eslint-disable-line no-console process.exit(1) diff --git a/test/fixtures/module/modules/basic/index.js b/test/fixtures/module/modules/basic/index.js new file mode 100755 index 0000000000..fb9243d0ed --- /dev/null +++ b/test/fixtures/module/modules/basic/index.js @@ -0,0 +1,17 @@ +const path = require('path') + +module.exports = async function basicModule (options) { + // Add simple vendor + this.addVendor('lodash') + + // Add a plugin + this.addPlugin(path.resolve(__dirname, 'reverse.js')) + + // Add simple api endpoint + this.addServerMiddleware({ + path: '/api', + handler (req, res, next) { + res.end('It works!') + } + }) +} diff --git a/test/fixtures/module/modules/basic/reverse.js b/test/fixtures/module/modules/basic/reverse.js new file mode 100755 index 0000000000..c2628cd9ad --- /dev/null +++ b/test/fixtures/module/modules/basic/reverse.js @@ -0,0 +1,11 @@ +// Simple Nuxt Plugin + +import Vue from 'vue' + +function $reverseStr (str) { + return str.split('').reverse().join('') +} + +Vue.prototype.$reverseStr = $reverseStr + +export default $reverseStr diff --git a/test/fixtures/module/nuxt.config.js b/test/fixtures/module/nuxt.config.js new file mode 100755 index 0000000000..8c102b63dc --- /dev/null +++ b/test/fixtures/module/nuxt.config.js @@ -0,0 +1,5 @@ +module.exports = { + modules: [ + '~modules/basic' + ] +} diff --git a/test/fixtures/module/package.json b/test/fixtures/module/package.json new file mode 100755 index 0000000000..a9924b8220 --- /dev/null +++ b/test/fixtures/module/package.json @@ -0,0 +1,5 @@ +{ + "name": "module", + "version": "1.0.0", + "dependencies": {} +} diff --git a/test/fixtures/module/pages/index.vue b/test/fixtures/module/pages/index.vue new file mode 100755 index 0000000000..33eaab7339 --- /dev/null +++ b/test/fixtures/module/pages/index.vue @@ -0,0 +1,5 @@ + diff --git a/test/module.test.js b/test/module.test.js new file mode 100755 index 0000000000..8d388669a8 --- /dev/null +++ b/test/module.test.js @@ -0,0 +1,43 @@ +import test from 'ava' +import { resolve } from 'path' +import rp from 'request-promise-native' + +const port = 4000 +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 Nuxt = require('../') + const rootDir = resolve(__dirname, 'fixtures/module') + let config = require(resolve(rootDir, 'nuxt.config.js')) + config.rootDir = rootDir + config.dev = false + nuxt = await new Nuxt(config) + await nuxt.build() + server = new nuxt.Server(nuxt) + server.listen(port, 'localhost') +}) + +test('Vendor', async t => { + t.true(nuxt.options.build.vendor.indexOf('lodash') !== -1, 'lodash added to config') +}) + +test('Plugin', async t => { + t.true(nuxt.options.plugins[0].src.startsWith('~/.nuxt/basic.reverse'), 'plugin added to config') + const { html } = await nuxt.renderRoute('/') + t.true(html.includes('

TXUN

'), 'plugin works') +}) + +test('Middleware', async t => { + let response = await rp(url('/api')) + t.is(response, 'It works!', '/api response is correct') +}) + +// Close server and ask nuxt to stop listening to file changes +test.after('Closing server and nuxt.js', t => { + server.close() + nuxt.close() +})