From 93696eb60b51d4a5c1552a3e695674ac21ca72b0 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Wed, 16 May 2018 08:10:20 +0100 Subject: [PATCH] Revert "test: bring cli test back" --- lib/common/utils.js | 23 ---------- test/fixtures/cli/cli.test.js | 27 ------------ test/fixtures/cli/nuxt.config.js | 22 ---------- test/fixtures/cli/pages/index.vue | 3 -- test/unit/cli.test.js | 71 +++++++++++++++++++++++-------- test/unit/utils.test.js | 5 --- 6 files changed, 53 insertions(+), 98 deletions(-) delete mode 100644 test/fixtures/cli/cli.test.js delete mode 100644 test/fixtures/cli/nuxt.config.js delete mode 100644 test/fixtures/cli/pages/index.vue diff --git a/lib/common/utils.js b/lib/common/utils.js index 79d9041145..0045b11ec9 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -13,29 +13,6 @@ export const waitFor = function waitFor(ms) { return new Promise(resolve => setTimeout(resolve, ms || 0)) } -/** - * Prepare an object to pass to the createSportsSelectionView function - * @param {Function} condition return true to stop the waiting - * @param {Number} duration seconds totally wait - * @param {Number} interval milliseconds interval to check the condition - * - * @returns {Boolean} true: timeout, false: condition becomes true within total time - */ -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 -} - async function promiseFinally(fn, finalFn) { let result try { diff --git a/test/fixtures/cli/cli.test.js b/test/fixtures/cli/cli.test.js deleted file mode 100644 index 94b19dba01..0000000000 --- a/test/fixtures/cli/cli.test.js +++ /dev/null @@ -1,27 +0,0 @@ -import { exec } from 'child_process' -import { resolve } from 'path' -import { promisify } from 'util' - -const execify = promisify(exec) -const rootDir = __dirname -const nuxtBin = resolve(__dirname, '..', '..', '..', 'bin', 'nuxt') - -describe('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({ - stdout: expect.stringContaining('Could not load config file: config.js') - }) - }) - - test('nuxt generate', async () => { - const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`) - - expect(stdout.includes('Generated successfully')).toBe(true) - }) -}) diff --git a/test/fixtures/cli/nuxt.config.js b/test/fixtures/cli/nuxt.config.js deleted file mode 100644 index 976d014c7b..0000000000 --- a/test/fixtures/cli/nuxt.config.js +++ /dev/null @@ -1,22 +0,0 @@ -import consola from 'consola' - -export default { - hooks(hook) { - hook('build:done', builder => { - consola.success('Compiled successfully') - }) - hook('generate:done', (generator, errors) => { - if (!errors || errors.length === 0) { - consola.success('Generated successfully') - } else { - consola.error('Generated failed') - } - }) - hook('listen', (server, { port, host }) => { - consola.success(`Listening on http://${host}:${port}`) - }) - }, - generate: { - dir: '.nuxt-generate' - } -} diff --git a/test/fixtures/cli/pages/index.vue b/test/fixtures/cli/pages/index.vue deleted file mode 100644 index 82cb6da1cd..0000000000 --- a/test/fixtures/cli/pages/index.vue +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/test/unit/cli.test.js b/test/unit/cli.test.js index c9d2d65392..04da799a56 100644 --- a/test/unit/cli.test.js +++ b/test/unit/cli.test.js @@ -1,29 +1,48 @@ -import { spawn } from 'child_process' +import { exec, spawn } from 'child_process' import { resolve } from 'path' -import { getPort, Utils, rp } from '../utils' +import { promisify } from 'util' +import { Utils, rp } from '../utils' + +const execify = promisify(exec) +const rootDir = resolve(__dirname, '..', 'fixtures/basic') let port -const rootDir = resolve(__dirname, '..', 'fixtures/cli') - const url = route => 'http://localhost:' + port + route const nuxtBin = resolve(__dirname, '..', '..', 'bin', 'nuxt') -describe('cli', () => { +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') + }) + }) + test('nuxt start', async () => { let stdout = '' + // let stderr = '' let error let exitCode const env = process.env - env.PORT = port = await getPort() + env.PORT = port - const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env }) + const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env: env }) nuxtStart.stdout.on('data', data => { stdout += data }) + nuxtStart.stderr.on('data', data => { + // stderr += data + }) + nuxtStart.on('error', err => { error = err }) @@ -32,27 +51,37 @@ describe('cli', () => { exitCode = code }) - // Wait max 20s for the starting - let timeout = await Utils.waitUntil(() => stdout.includes('Listening on')) + // Give the process max 20s to start + let iterator = 0 + while (!stdout.includes('OPEN') && iterator < 80) { + await Utils.waitFor(250) + iterator++ + } - if (timeout === true) { - error = 'server failed to start successfully in 20 seconds' + if (iterator === 80) { + test.log('WARN: server failed to start successfully in 20 seconds') } expect(error).toBe(undefined) - expect(stdout.includes('Listening on')).toBe(true) + expect(stdout.includes('OPEN')).toBe(true) - const html = await rp(url('/')) - expect(html).toMatch(('
CLI Test
')) + const html = await rp(url('/users/1')) + expect(html.includes('

User: 1

')).toBe(true) nuxtStart.kill() // Wait max 10s for the process to be killed - timeout = await Utils.waitUntil(() => exitCode !== undefined, 10) + iterator = 0 + // eslint-disable-next-line no-unmodified-loop-condition + while (exitCode === undefined && iterator < 40) { + await Utils.waitFor(250) + iterator++ + } - if (timeout === true) { - console.warn( // eslint-disable-line no-console - `we were unable to automatically kill the child process with pid: ${ + if (iterator >= 40) { + // eslint-disable-line no-console + test.log( + `WARN: we were unable to automatically kill the child process with pid: ${ nuxtStart.pid }` ) @@ -60,4 +89,10 @@ describe('cli', () => { expect(exitCode).toBe(null) }) + + test('nuxt generate', async () => { + const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`) + + expect(stdout.includes('vue-ssr-client-manifest.json')).toBe(true) + }) }) diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js index 81ec6d4f95..1701177102 100644 --- a/test/unit/utils.test.js +++ b/test/unit/utils.test.js @@ -23,11 +23,6 @@ describe('utils', () => { await Utils.waitFor() }) - test('waitUntil', async () => { - expect(await Utils.waitUntil(() => true, 0.1, 100)).toBe(false) - expect(await Utils.waitUntil(() => false, 0.1, 100)).toBe(true) - }) - test('timeout (promise)', async () => { const result = await Utils.timeout(Promise.resolve('time not run out'), 100) expect(result).toBe('time not run out')