Nuxt/test/basic.dev.test.js

74 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-12-21 19:51:43 +00:00
import test from 'ava'
import { resolve } from 'path'
2017-12-05 10:36:54 +00:00
import stdMocks from 'std-mocks'
2017-07-03 11:28:38 +00:00
// import rp from 'request-promise-native'
import { Nuxt, Builder, Utils } from '..'
2017-12-05 10:36:54 +00:00
import { truncateSync, readFileSync, writeFileSync } from 'fs'
2017-06-16 12:49:35 +00:00
2017-05-21 19:00:01 +00:00
const port = 4001
2016-12-21 19:51:43 +00:00
const url = (route) => 'http://localhost:' + port + route
2017-12-05 10:36:54 +00:00
const rootDir = resolve(__dirname, 'fixtures/basic')
const pluginPath = resolve(rootDir, 'plugins', 'watch.js')
const pluginContent = readFileSync(pluginPath)
2016-12-21 19:51:43 +00:00
let nuxt = null
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const options = {
2017-12-05 10:36:54 +00:00
rootDir,
dev: true,
plugins: [
'~/plugins/watch.js'
]
2016-12-21 19:51:43 +00:00
}
nuxt = new Nuxt(options)
2017-06-18 17:33:23 +00:00
await new Builder(nuxt).build()
2017-06-20 11:44:47 +00:00
await nuxt.listen(port, 'localhost')
2016-12-21 19:51:43 +00:00
})
2017-12-05 10:36:54 +00:00
test('remove mixins in live reloading', async t => {
stdMocks.use()
await nuxt.renderRoute(url('/'))
t.true(stdMocks.flush().stdout.some(v => v === 'I am mixin\n'))
truncateSync(pluginPath)
await new Promise(async (resolve, reject) => {
let waitTimes = 0
while (!stdMocks.flush().stdout.some(v => ~v.indexOf('Compiled successfully'))) {
await Utils.waitFor(100) && waitTimes++
if (waitTimes === 20) {
reject(Error('Dev server doesn\'t reload after 2000ms'))
}
}
resolve()
})
await nuxt.renderRoute(url('/'))
t.false(stdMocks.flush().stdout.some(v => v === 'I am mixin\n'))
stdMocks.restore()
})
2016-12-21 19:51:43 +00:00
test('/stateless', async t => {
const window = await nuxt.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>My component!</h1>'))
})
2017-07-03 11:17:22 +00:00
// test('/_nuxt/test.hot-update.json should returns empty html', async t => {
// try {
// await rp(url('/_nuxt/test.hot-update.json'))
// } catch (err) {
// t.is(err.statusCode, 404)
// t.is(err.response.body, '')
// }
// })
2017-05-16 13:12:30 +00:00
2016-12-21 19:51:43 +00:00
// Close server and ask nuxt to stop listening to file changes
2017-06-18 17:33:23 +00:00
test.after('Closing server and nuxt.js', async t => {
2017-12-05 10:36:54 +00:00
writeFileSync(pluginPath, pluginContent)
2017-06-20 12:55:52 +00:00
await nuxt.close()
2016-12-21 19:51:43 +00:00
})