diff --git a/test/unit/cli.test.js b/test/unit/cli.test.js index 39977eb686..6959aa0187 100644 --- a/test/unit/cli.test.js +++ b/test/unit/cli.test.js @@ -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(('
CLI Test
')) diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js deleted file mode 100644 index 140465218b..0000000000 --- a/test/unit/utils.test.js +++ /dev/null @@ -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) - }) -}) diff --git a/test/utils/index.js b/test/utils/index.js index afa91cd8bc..85e19b68d5 100644 --- a/test/utils/index.js +++ b/test/utils/index.js @@ -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