Nuxt/test/module.test.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-05-14 22:33:31 +00:00
import test from 'ava'
2017-11-19 15:02:44 +00:00
import stdMocks from 'std-mocks'
2017-06-12 20:15:28 +00:00
import { resolve, normalize } from 'path'
2017-05-14 22:33:31 +00:00
import rp from 'request-promise-native'
2017-06-20 11:44:47 +00:00
import { Nuxt, Builder } from '../index.js'
2017-05-14 22:33:31 +00:00
2017-05-21 19:00:01 +00:00
const port = 4006
2017-05-14 22:33:31 +00:00
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
2017-11-19 14:35:11 +00:00
let builder = null
2017-11-19 15:02:44 +00:00
let builtErr = null
2017-05-14 22:33:31 +00:00
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const rootDir = resolve(__dirname, 'fixtures/module')
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = new Nuxt(config)
2017-11-19 14:35:11 +00:00
builder = new Builder(nuxt)
2017-11-19 15:02:44 +00:00
stdMocks.use({
stdout: false,
stderr: true
})
2017-11-19 14:35:11 +00:00
await builder.build()
2017-11-19 15:02:44 +00:00
stdMocks.restore()
builtErr = stdMocks.flush().stderr
2017-06-20 11:44:47 +00:00
await nuxt.listen(port, 'localhost')
2017-05-14 22:33:31 +00:00
})
test('Vendor', async t => {
t.true(nuxt.options.build.vendor.indexOf('lodash') !== -1, 'lodash added to config')
})
test('Plugin', async t => {
2017-06-13 20:15:06 +00:00
t.true(normalize(nuxt.options.plugins[0].src)
.includes(normalize('fixtures/module/.nuxt/basic.reverse.')), 'plugin added to config')
2017-05-14 22:33:31 +00:00
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')
})
2017-10-30 21:39:08 +00:00
test('Hooks', async t => {
2017-07-17 19:38:02 +00:00
t.is(nuxt.__module_hook, 1)
t.is(nuxt.__renderer_hook, 2)
t.is(nuxt.__builder_hook, 3)
})
2017-11-19 14:35:11 +00:00
test('Hooks - Functional', async t => {
t.true(nuxt.__ready_called__)
t.true(builder.__build_done__)
})
2017-11-19 15:02:44 +00:00
test('Hooks - Error', async t => {
const errors = builtErr.filter(value => value.indexOf('build:extendRoutes') >= 0)
t.true(errors.length === 1)
})
2017-05-14 22:33:31 +00:00
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
})