[module] Tests

This commit is contained in:
Pooya Parsa 2017-05-15 03:03:31 +04:30
parent 59ece4978e
commit 632a94a9c6
8 changed files with 90 additions and 2 deletions

View File

@ -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)

View File

@ -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)

17
test/fixtures/module/modules/basic/index.js vendored Executable file
View File

@ -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!')
}
})
}

11
test/fixtures/module/modules/basic/reverse.js vendored Executable file
View File

@ -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

5
test/fixtures/module/nuxt.config.js vendored Executable file
View File

@ -0,0 +1,5 @@
module.exports = {
modules: [
'~modules/basic'
]
}

5
test/fixtures/module/package.json vendored Executable file
View File

@ -0,0 +1,5 @@
{
"name": "module",
"version": "1.0.0",
"dependencies": {}
}

5
test/fixtures/module/pages/index.vue vendored Executable file
View File

@ -0,0 +1,5 @@
<template>
<div>
<h1>{{ $reverseStr('NUXT') }}</h1>
</div>
</template>

43
test/module.test.js Executable file
View File

@ -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('<h1>TXUN</h1>'), '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()
})