Nuxt/test/cli.test.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

import { promisify } from 'util'
import test from 'ava'
import { resolve, sep } from 'path'
import rp from 'request-promise-native'
import { exec, spawn } from 'child_process'
import { Utils } from '..'
const execify = promisify(exec)
const rootDir = resolve(__dirname, 'fixtures/basic')
const port = 4011
2018-01-11 16:33:30 +00:00
const url = route => 'http://localhost:' + port + route
2018-01-16 07:46:43 +00:00
const nuxtBin = resolve(__dirname, '..', 'bin', 'nuxt')
2018-01-16 07:46:43 +00:00
test.serial('nuxt build', async t => {
const { stdout, stderr } = await execify(`node ${nuxtBin} build ${rootDir}`)
t.true(stdout.includes('server-bundle.json'))
t.true(stderr.includes('Building done'))
})
2018-01-16 07:46:43 +00:00
test.serial('nuxt start', async t => {
let stdout = ''
2018-01-16 07:46:43 +00:00
// let stderr = ''
let error
let exitCode
const env = process.env
env.PORT = port
2018-01-16 07:46:43 +00:00
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env: env })
2018-01-11 16:33:30 +00:00
nuxtStart.stdout.on('data', data => {
stdout += data
})
2018-01-11 16:33:30 +00:00
nuxtStart.stderr.on('data', data => {
2018-01-16 07:46:43 +00:00
// stderr += data
})
2018-01-11 16:33:30 +00:00
nuxtStart.on('error', err => {
error = err
})
2018-01-11 16:33:30 +00:00
nuxtStart.on('close', code => {
exitCode = code
})
// Give the process max 10s to start
let iterator = 0
while (!stdout.includes('OPEN') && iterator < 40) {
await Utils.waitFor(250)
iterator++
}
t.is(error, undefined)
t.true(stdout.includes('OPEN'))
const html = await rp(url('/users/1'))
t.true(html.includes('<h1>User: 1</h1>'))
nuxtStart.kill()
// Wait max 10s for the process to be killed
iterator = 0
2018-01-11 19:07:05 +00:00
// eslint-disable-next-line no-unmodified-loop-condition
2018-01-11 16:33:30 +00:00
while (exitCode === undefined && iterator < 40) {
await Utils.waitFor(250)
iterator++
}
if (iterator >= 40) {
2018-01-11 16:33:30 +00:00
t.log(
`WARN: we were unable to automatically kill the child process with pid: ${
nuxtStart.pid
}`
)
}
t.is(exitCode, null)
})
2018-01-16 07:46:43 +00:00
test.serial('nuxt generate', async t => {
const { stdout, stderr } = await execify(`node ${nuxtBin} generate ${rootDir}`)
t.true(stdout.includes('server-bundle.json'))
t.true(stderr.includes('Destination folder cleaned'))
t.true(stderr.includes('Static & build files copied'))
t.true(stderr.includes(`Generate file: ${sep}users${sep}1${sep}index.html`))
t.true(stdout.includes('Generate errors summary:'))
t.true(stderr.includes('Generate done'))
})