From 67d560114223d97ecc8c7537acc02829ad507806 Mon Sep 17 00:00:00 2001 From: Pim Date: Fri, 11 Oct 2019 09:35:10 +0200 Subject: [PATCH] test: refactor cli integration tests (#6537) --- jest.config.js | 9 +++++++++ packages/cli/src/index.js | 14 +++++++------ test/fixtures/cli/cli.build.config.js | 10 +++++++-- test/fixtures/cli/cli.build.test.js | 29 +++++++++++++-------------- test/fixtures/cli/cli.gen.config.js | 9 +++++++-- test/fixtures/cli/cli.gen.test.js | 27 +++++++++++++++---------- 6 files changed, 62 insertions(+), 36 deletions(-) diff --git a/jest.config.js b/jest.config.js index 7df0f08215..c6daa27618 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,8 @@ +const fs = require('fs') +const path = require('path') + +const corePackages = fs.readdirSync(path.resolve(__dirname, 'packages')) + module.exports = { testEnvironment: 'node', @@ -48,6 +53,10 @@ module.exports = { 'json' ], + moduleNameMapper: { + [`@nuxt/(${corePackages.join('|')})(/?.*)$`]: '/packages/$1/src/$2' + }, + reporters: [ 'default', ['jest-junit', { outputDirectory: 'reports/junit' }] diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 45ccf9e97c..095d5b402d 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -1,10 +1,12 @@ -import * as _commands from './commands' -import * as _imports from './imports' -import * as _options from './options' +import * as commands from './commands' +import * as imports from './imports' +import * as options from './options' -export const commands = _commands -export const imports = _imports -export const options = _options +export { + commands, + imports, + options +} export { default as NuxtCommand } from './command' export { default as setup } from './setup' diff --git a/test/fixtures/cli/cli.build.config.js b/test/fixtures/cli/cli.build.config.js index 1905424a92..47c43979a0 100644 --- a/test/fixtures/cli/cli.build.config.js +++ b/test/fixtures/cli/cli.build.config.js @@ -1,11 +1,17 @@ +import consola from 'consola' + export default { test: true, + mode: 'spa', hooks (hook) { hook('build:done', () => { - process.stdout.write('Compiled successfully') + consola.log('Compiled successfully') }) hook('listen', (server, { port, host }) => { - process.stdout.write(`Listening on http://${host}:${port}`) + consola.log(`Listening on http://${host}:${port}`) }) + }, + build: { + terser: false } } diff --git a/test/fixtures/cli/cli.build.test.js b/test/fixtures/cli/cli.build.test.js index 91536d3e1b..a49f09f01d 100644 --- a/test/fixtures/cli/cli.build.test.js +++ b/test/fixtures/cli/cli.build.test.js @@ -1,21 +1,20 @@ -import { exec } from 'child_process' -import { resolve } from 'path' -import { promisify } from 'util' - -const execify = promisify(exec) -const rootDir = __dirname -const nuxtBin = resolve(__dirname, '../../../packages/cli/bin/nuxt.js') +import { NuxtCommand, commands } from '@nuxt/cli' +import consola from 'consola' describe('cli build', () => { - test.skip('nuxt build', async () => { - const { stdout } = await execify(`node -r esm ${nuxtBin} build ${rootDir} -c cli.build.config.js`) + test('nuxt build', async () => { + const buildCommand = await commands.default('build') - expect(stdout.includes('Compiled successfully')).toBe(true) - }, 80000) + const argv = [ + __dirname, + '--no-force-exit', + '-c', + 'cli.build.config.js' + ] - test.skip('nuxt build -> error config', async () => { - await expect(execify(`node -r esm ${nuxtBin} build ${rootDir} -c config.js`)).rejects.toMatchObject({ - stderr: expect.stringContaining('Could not load config file: config.js') - }) + const cmd = new NuxtCommand(buildCommand, argv) + await expect(cmd.run()).resolves.toBeUndefined() + + expect(consola.log).toBeCalledWith('Compiled successfully') }) }) diff --git a/test/fixtures/cli/cli.gen.config.js b/test/fixtures/cli/cli.gen.config.js index 76a8d61432..906efad1f3 100644 --- a/test/fixtures/cli/cli.gen.config.js +++ b/test/fixtures/cli/cli.gen.config.js @@ -1,3 +1,5 @@ +import consola from 'consola' + export default { test: true, buildDir: '.nuxt-generate/build', @@ -7,10 +9,13 @@ export default { hooks (hook) { hook('generate:done', (generator, errors) => { if (!errors || errors.length === 0) { - process.stdout.write('Generated successfully') + consola.log('Generated successfully') } else { - process.stderr.write('Generated failed') + consola.log('Generated failed') } }) + }, + build: { + terser: false } } diff --git a/test/fixtures/cli/cli.gen.test.js b/test/fixtures/cli/cli.gen.test.js index 2356c292ba..060cb60535 100644 --- a/test/fixtures/cli/cli.gen.test.js +++ b/test/fixtures/cli/cli.gen.test.js @@ -1,15 +1,20 @@ -import { exec } from 'child_process' -import { resolve } from 'path' -import { promisify } from 'util' - -const execify = promisify(exec) -const rootDir = __dirname -const nuxtBin = resolve(__dirname, '../../../packages/cli/bin/nuxt.js') +import { NuxtCommand, commands } from '@nuxt/cli' +import consola from 'consola' describe('cli generate', () => { - test.skip('nuxt generate', async () => { - const { stdout } = await execify(`node -r esm ${nuxtBin} generate ${rootDir} -c cli.gen.config.js`) + test('nuxt generate', async () => { + const generateCommand = await commands.default('generate') - expect(stdout.includes('Generated successfully')).toBe(true) - }, 80000) + const argv = [ + __dirname, + '--no-force-exit', + '-c', + 'cli.gen.config.js' + ] + + const cmd = new NuxtCommand(generateCommand, argv) + await expect(cmd.run()).resolves.toBeUndefined() + + expect(consola.log).toBeCalledWith('Generated successfully') + }) })