Nuxt/test/unit/cli.test.js

64 lines
1.5 KiB
JavaScript
Raw Normal View History

import { spawn } from 'child_process'
2018-03-11 23:37:38 +00:00
import { resolve } from 'path'
import { getPort, rp, waitUntil } from '../utils'
2018-05-15 11:45:09 +00:00
2018-05-16 07:10:20 +00:00
let port
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
2018-01-11 16:33:30 +00:00
const url = route => 'http://localhost:' + port + route
2018-03-19 10:06:45 +00:00
const nuxtBin = resolve(__dirname, '..', '..', 'bin', 'nuxt')
describe('cli', () => {
2018-03-18 19:31:32 +00:00
test('nuxt start', async () => {
let stdout = ''
let error
let exitCode
const env = process.env
env.PORT = port = await getPort()
2018-03-18 19:31:32 +00:00
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env })
2018-03-18 19:31:32 +00:00
nuxtStart.stdout.on('data', data => {
stdout += data
})
nuxtStart.on('error', err => {
error = err
})
nuxtStart.on('close', code => {
exitCode = code
})
// Wait max 20s for the starting
let timeout = await waitUntil(() => stdout.includes('Listening on'))
2018-03-18 19:31:32 +00:00
if (timeout === true) {
error = 'server failed to start successfully in 20 seconds'
2018-03-18 19:31:32 +00:00
}
expect(error).toBe(undefined)
expect(stdout.includes('Listening on')).toBe(true)
2018-03-18 19:31:32 +00:00
const html = await rp(url('/'))
expect(html).toMatch(('<div>CLI Test</div>'))
2018-03-18 19:31:32 +00:00
nuxtStart.kill()
// Wait max 10s for the process to be killed
timeout = await waitUntil(() => exitCode !== undefined, 10)
2018-03-18 19:31:32 +00:00
if (timeout === true) {
console.warn( // eslint-disable-line no-console
`we were unable to automatically kill the child process with pid: ${
2018-03-18 19:31:32 +00:00
nuxtStart.pid
}`
)
}
expect(exitCode).toBe(null)
})
})