2017-12-12 07:43:56 +00:00
|
|
|
import { promisify } from 'util'
|
2017-11-23 22:31:54 +00:00
|
|
|
import test from 'ava'
|
2018-03-11 23:37:38 +00:00
|
|
|
import { resolve } from 'path'
|
2017-11-23 22:31:54 +00:00
|
|
|
import rp from 'request-promise-native'
|
|
|
|
import { exec, spawn } from 'child_process'
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Utils } from '..'
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
const execify = promisify(exec)
|
2017-11-23 22:31:54 +00:00
|
|
|
const rootDir = resolve(__dirname, 'fixtures/basic')
|
|
|
|
|
|
|
|
const port = 4011
|
2018-01-11 16:33:30 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-16 07:46:43 +00:00
|
|
|
const nuxtBin = resolve(__dirname, '..', 'bin', 'nuxt')
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-16 07:46:43 +00:00
|
|
|
test.serial('nuxt build', async t => {
|
|
|
|
const { stdout, stderr } = await execify(`node ${nuxtBin} build ${rootDir}`)
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
t.true(stdout.includes('server-bundle.json'))
|
|
|
|
t.true(stderr.includes('Building done'))
|
|
|
|
})
|
|
|
|
|
2018-01-27 16:23:05 +00:00
|
|
|
test.serial('nuxt build -> error config', async t => {
|
|
|
|
const { stderr } = await t.throws(execify(`node ${nuxtBin} build ${rootDir} -c config.js`))
|
|
|
|
t.true(stderr.includes('Could not load config file'))
|
|
|
|
})
|
|
|
|
|
2018-01-16 07:46:43 +00:00
|
|
|
test.serial('nuxt start', async t => {
|
2017-11-23 22:31:54 +00:00
|
|
|
let stdout = ''
|
2018-01-16 07:46:43 +00:00
|
|
|
// let stderr = ''
|
2017-11-23 22:31:54 +00:00
|
|
|
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 })
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-11 16:33:30 +00:00
|
|
|
nuxtStart.stdout.on('data', data => {
|
2017-11-23 22:31:54 +00:00
|
|
|
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
|
2017-11-23 22:31:54 +00:00
|
|
|
})
|
|
|
|
|
2018-01-11 16:33:30 +00:00
|
|
|
nuxtStart.on('error', err => {
|
2017-11-23 22:31:54 +00:00
|
|
|
error = err
|
|
|
|
})
|
|
|
|
|
2018-01-11 16:33:30 +00:00
|
|
|
nuxtStart.on('close', code => {
|
2017-11-23 22:31:54 +00:00
|
|
|
exitCode = code
|
|
|
|
})
|
|
|
|
|
2018-03-09 02:05:36 +00:00
|
|
|
// Give the process max 20s to start
|
2017-11-23 22:31:54 +00:00
|
|
|
let iterator = 0
|
2018-03-09 02:05:36 +00:00
|
|
|
while (!stdout.includes('OPEN') && iterator < 80) {
|
2017-11-23 22:31:54 +00:00
|
|
|
await Utils.waitFor(250)
|
|
|
|
iterator++
|
|
|
|
}
|
|
|
|
|
2018-03-09 02:09:22 +00:00
|
|
|
if (iterator === 80) {
|
|
|
|
t.log('WARN: server failed to start successfully in 20 seconds')
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
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) {
|
2017-11-23 22:31:54 +00:00
|
|
|
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
|
|
|
|
}`
|
|
|
|
)
|
2017-11-23 22:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.is(exitCode, null)
|
|
|
|
})
|
|
|
|
|
2018-01-16 07:46:43 +00:00
|
|
|
test.serial('nuxt generate', async t => {
|
2018-03-11 23:37:38 +00:00
|
|
|
const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`)
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
t.true(stdout.includes('server-bundle.json'))
|
|
|
|
})
|