mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
refactor(test): change cli.test to be more accurate (#4957)
This commit is contained in:
parent
4e30bda44e
commit
68f6880f54
@ -1,7 +1,7 @@
|
|||||||
import { resolve, join } from 'path'
|
import { resolve, join } from 'path'
|
||||||
import { spawn } from 'cross-spawn'
|
import { spawn } from 'cross-spawn'
|
||||||
import { writeFileSync } from 'fs-extra'
|
import { writeFileSync } from 'fs-extra'
|
||||||
import { getPort, rp, waitUntil, waitFor } from '../utils'
|
import { getPort, rp, waitFor } from '../utils'
|
||||||
|
|
||||||
let port
|
let port
|
||||||
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
|
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 nuxtBin = resolve(__dirname, '../../packages/cli/bin/nuxt-cli.js')
|
||||||
const spawnNuxt = (command, opts) => spawn(nuxtBin, [command, rootDir], opts)
|
const spawnNuxt = (command, opts) => spawn(nuxtBin, [command, rootDir], opts)
|
||||||
|
|
||||||
const close = async (nuxtInt) => {
|
const start = (cmd, env, cb) => {
|
||||||
nuxtInt.kill('SIGKILL')
|
return new Promise((resolve) => {
|
||||||
// Wait max 10s for the process to be killed
|
const nuxt = spawnNuxt(cmd, { env, detached: true })
|
||||||
if (await waitUntil(() => nuxtInt.killed, 10)) {
|
const listener = (data) => {
|
||||||
// eslint-disable-next-line no-console
|
if (data.includes(`${port}`)) {
|
||||||
console.warn(`Unable to close process with pid: ${nuxtInt.pid}`)
|
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 () => {
|
test('nuxt dev', async () => {
|
||||||
let stdout = ''
|
|
||||||
const { env } = process
|
const { env } = process
|
||||||
env.PORT = port = await getPort()
|
env.PORT = port = await getPort()
|
||||||
|
|
||||||
const nuxtDev = spawnNuxt('dev', { env })
|
const nuxtDev = await start('dev', env)
|
||||||
nuxtDev.stdout.on('data', (data) => { stdout += data })
|
|
||||||
|
|
||||||
// Wait max 20s for the starting
|
|
||||||
await waitUntil(() => stdout.includes(`${port}`))
|
|
||||||
|
|
||||||
// Change file specified in `watchers` (nuxt.config.js)
|
// Change file specified in `watchers` (nuxt.config.js)
|
||||||
const customFilePath = join(rootDir, 'custom.file')
|
const customFilePath = join(rootDir, 'custom.file')
|
||||||
@ -49,7 +58,6 @@ describe.skip('cli', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('nuxt start', async () => {
|
test('nuxt start', async () => {
|
||||||
let stdout = ''
|
|
||||||
let error
|
let error
|
||||||
|
|
||||||
const { env } = process
|
const { env } = process
|
||||||
@ -57,21 +65,14 @@ describe.skip('cli', () => {
|
|||||||
|
|
||||||
await new Promise((resolve) => {
|
await new Promise((resolve) => {
|
||||||
const nuxtBuild = spawnNuxt('build', { env })
|
const nuxtBuild = spawnNuxt('build', { env })
|
||||||
nuxtBuild.on('close', () => { resolve() })
|
nuxtBuild.on('close', resolve)
|
||||||
})
|
})
|
||||||
|
|
||||||
const nuxtStart = spawnNuxt('start', { env })
|
const nuxtStart = await start('start', env, (nuxtStart) => {
|
||||||
|
nuxtStart.on('error', (err) => { error = err })
|
||||||
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'
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error).toBe(undefined)
|
expect(error).toBe(undefined)
|
||||||
expect(stdout).toContain('Listening on')
|
|
||||||
|
|
||||||
const html = await rp(url('/'))
|
const html = await rp(url('/'))
|
||||||
expect(html).toMatch(('<div>CLI Test</div>'))
|
expect(html).toMatch(('<div>CLI Test</div>'))
|
||||||
|
@ -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)
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,5 +1,4 @@
|
|||||||
import klawSync from 'klaw-sync'
|
import klawSync from 'klaw-sync'
|
||||||
import { waitFor } from '../../packages/utils'
|
|
||||||
|
|
||||||
export { getNuxtConfig } from '../../packages/config'
|
export { getNuxtConfig } from '../../packages/config'
|
||||||
export { default as getPort } from 'get-port'
|
export { default as getPort } from 'get-port'
|
||||||
@ -7,24 +6,6 @@ export { default as rp } from 'request-promise-native'
|
|||||||
|
|
||||||
export * from './nuxt'
|
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 = {}) {
|
export const listPaths = function listPaths(dir, pathsBefore = [], options = {}) {
|
||||||
if (Array.isArray(pathsBefore) && pathsBefore.length) {
|
if (Array.isArray(pathsBefore) && pathsBefore.length) {
|
||||||
// Only return files that didn't exist before building
|
// Only return files that didn't exist before building
|
||||||
|
Loading…
Reference in New Issue
Block a user