Nuxt/test/unit/cli.test.js

99 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-05-16 07:10:20 +00:00
import { exec, spawn } from 'child_process'
2018-03-11 23:37:38 +00:00
import { resolve } from 'path'
2018-05-16 07:10:20 +00:00
import { promisify } from 'util'
import { Utils, rp } from '../utils'
2018-05-16 07:10:20 +00:00
const execify = promisify(exec)
const rootDir = resolve(__dirname, '..', 'fixtures/basic')
2018-05-15 11:45:09 +00:00
2018-05-16 07:10:20 +00:00
let port
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')
2018-05-16 07:10:20 +00:00
describe.skip('cli', () => {
test('nuxt build', async () => {
const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`)
expect(stdout.includes('Compiled successfully')).toBe(true)
})
test('nuxt build -> error config', async () => {
await expect(execify(`node ${nuxtBin} build ${rootDir} -c config.js`)).rejects.toMatchObject({
stderr: expect.stringContaining('Could not load config file')
})
})
2018-03-18 19:31:32 +00:00
test('nuxt start', async () => {
let stdout = ''
2018-05-16 07:10:20 +00:00
// let stderr = ''
2018-03-18 19:31:32 +00:00
let error
let exitCode
const env = process.env
2018-05-16 07:10:20 +00:00
env.PORT = port
2018-03-18 19:31:32 +00:00
2018-05-16 07:10:20 +00:00
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env: env })
2018-03-18 19:31:32 +00:00
nuxtStart.stdout.on('data', data => {
stdout += data
})
2018-05-16 07:10:20 +00:00
nuxtStart.stderr.on('data', data => {
// stderr += data
})
2018-03-18 19:31:32 +00:00
nuxtStart.on('error', err => {
error = err
})
nuxtStart.on('close', code => {
exitCode = code
})
2018-05-16 07:10:20 +00:00
// Give the process max 20s to start
let iterator = 0
while (!stdout.includes('OPEN') && iterator < 80) {
await Utils.waitFor(250)
iterator++
}
2018-03-18 19:31:32 +00:00
2018-05-16 07:10:20 +00:00
if (iterator === 80) {
test.log('WARN: server failed to start successfully in 20 seconds')
2018-03-18 19:31:32 +00:00
}
expect(error).toBe(undefined)
2018-05-16 07:10:20 +00:00
expect(stdout.includes('OPEN')).toBe(true)
2018-03-18 19:31:32 +00:00
2018-05-16 07:10:20 +00:00
const html = await rp(url('/users/1'))
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
2018-03-18 19:31:32 +00:00
nuxtStart.kill()
// Wait max 10s for the process to be killed
2018-05-16 07:10:20 +00:00
iterator = 0
// eslint-disable-next-line no-unmodified-loop-condition
while (exitCode === undefined && iterator < 40) {
await Utils.waitFor(250)
iterator++
}
2018-03-18 19:31:32 +00:00
2018-05-16 07:10:20 +00:00
if (iterator >= 40) {
// eslint-disable-line no-console
test.log(
`WARN: 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)
})
2018-05-16 07:10:20 +00:00
test('nuxt generate', async () => {
const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`)
expect(stdout.includes('vue-ssr-client-manifest.json')).toBe(true)
})
})