refactor(test): change cli.test to be more accurate (#4957)

This commit is contained in:
Xin Du (Clark) 2019-02-06 18:45:39 +00:00 committed by Pooya Parsa
parent 4e30bda44e
commit 68f6880f54
3 changed files with 28 additions and 54 deletions

View File

@ -1,7 +1,7 @@
import { resolve, join } from 'path'
import { spawn } from 'cross-spawn'
import { writeFileSync } from 'fs-extra'
import { getPort, rp, waitUntil, waitFor } from '../utils'
import { getPort, rp, waitFor } from '../utils'
let port
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
@ -11,26 +11,35 @@ const url = route => 'http://localhost:' + port + route
const nuxtBin = resolve(__dirname, '../../packages/cli/bin/nuxt-cli.js')
const spawnNuxt = (command, opts) => spawn(nuxtBin, [command, rootDir], opts)
const close = async (nuxtInt) => {
nuxtInt.kill('SIGKILL')
// Wait max 10s for the process to be killed
if (await waitUntil(() => nuxtInt.killed, 10)) {
// eslint-disable-next-line no-console
console.warn(`Unable to close process with pid: ${nuxtInt.pid}`)
}
const start = (cmd, env, cb) => {
return new Promise((resolve) => {
const nuxt = spawnNuxt(cmd, { env, detached: true })
const listener = (data) => {
if (data.includes(`${port}`)) {
nuxt.stdout.removeListener('data', listener)
resolve(nuxt)
}
}
if (typeof cb === 'function') {
cb(nuxt)
}
nuxt.stdout.on('data', listener)
})
}
describe.skip('cli', () => {
const close = (nuxt) => {
return new Promise((resolve) => {
nuxt.on('exit', resolve)
process.kill(-nuxt.pid)
})
}
describe.posix('cli', () => {
test('nuxt dev', async () => {
let stdout = ''
const { env } = process
env.PORT = port = await getPort()
const nuxtDev = spawnNuxt('dev', { env })
nuxtDev.stdout.on('data', (data) => { stdout += data })
// Wait max 20s for the starting
await waitUntil(() => stdout.includes(`${port}`))
const nuxtDev = await start('dev', env)
// Change file specified in `watchers` (nuxt.config.js)
const customFilePath = join(rootDir, 'custom.file')
@ -49,7 +58,6 @@ describe.skip('cli', () => {
})
test('nuxt start', async () => {
let stdout = ''
let error
const { env } = process
@ -57,21 +65,14 @@ describe.skip('cli', () => {
await new Promise((resolve) => {
const nuxtBuild = spawnNuxt('build', { env })
nuxtBuild.on('close', () => { resolve() })
nuxtBuild.on('close', resolve)
})
const nuxtStart = spawnNuxt('start', { env })
nuxtStart.stdout.on('data', (data) => { stdout += data })
nuxtStart.on('error', (err) => { error = err })
// Wait max 40s for the starting
if (await waitUntil(() => stdout.includes(`${port}`), 40)) {
error = 'server failed to start successfully in 40 seconds'
}
const nuxtStart = await start('start', env, (nuxtStart) => {
nuxtStart.on('error', (err) => { error = err })
})
expect(error).toBe(undefined)
expect(stdout).toContain('Listening on')
const html = await rp(url('/'))
expect(html).toMatch(('<div>CLI Test</div>'))

View File

@ -1,8 +0,0 @@
import { waitUntil } from '../utils'
describe('utils', () => {
test('waitUntil', async () => {
expect(await waitUntil(() => true, 0.1, 100)).toBe(false)
expect(await waitUntil(() => false, 0.1, 100)).toBe(true)
})
})

View File

@ -1,5 +1,4 @@
import klawSync from 'klaw-sync'
import { waitFor } from '../../packages/utils'
export { getNuxtConfig } from '../../packages/config'
export { default as getPort } from 'get-port'
@ -7,24 +6,6 @@ export { default as rp } from 'request-promise-native'
export * from './nuxt'
// Pauses execution for a determined amount of time (`duration`)
// until `condition` is met. Also allows specifying the `interval`
// at which the condition is checked during the waiting period.
export const waitUntil = async function waitUntil(condition, duration = 20, interval = 250) {
let iterator = 0
const steps = Math.floor(duration * 1000 / interval)
while (!condition() && iterator < steps) {
await waitFor(interval)
iterator++
}
if (iterator === steps) {
return true
}
return false
}
export const listPaths = function listPaths(dir, pathsBefore = [], options = {}) {
if (Array.isArray(pathsBefore) && pathsBefore.length) {
// Only return files that didn't exist before building