Skip to content

Commit

Permalink
test: refactor cli integration tests (#6537)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and pi0 committed Oct 11, 2019
1 parent a0ef4a3 commit 67d5601
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 36 deletions.
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const fs = require('fs')
const path = require('path')

const corePackages = fs.readdirSync(path.resolve(__dirname, 'packages'))

module.exports = {
testEnvironment: 'node',

Expand Down Expand Up @@ -48,6 +53,10 @@ module.exports = {
'json'
],

moduleNameMapper: {
[`@nuxt/(${corePackages.join('|')})(/?.*)$`]: '<rootDir>/packages/$1/src/$2'
},

reporters: [
'default',
['jest-junit', { outputDirectory: 'reports/junit' }]
Expand Down
14 changes: 8 additions & 6 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
10 changes: 8 additions & 2 deletions test/fixtures/cli/cli.build.config.js
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 14 additions & 15 deletions test/fixtures/cli/cli.build.test.js
Original file line number Diff line number Diff line change
@@ -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')

const argv = [
__dirname,
'--no-force-exit',
'-c',
'cli.build.config.js'
]

expect(stdout.includes('Compiled successfully')).toBe(true)
}, 80000)
const cmd = new NuxtCommand(buildCommand, argv)
await expect(cmd.run()).resolves.toBeUndefined()

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')
})
expect(consola.log).toBeCalledWith('Compiled successfully')
})
})
9 changes: 7 additions & 2 deletions test/fixtures/cli/cli.gen.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import consola from 'consola'

export default {
test: true,
buildDir: '.nuxt-generate/build',
Expand All @@ -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
}
}
27 changes: 16 additions & 11 deletions test/fixtures/cli/cli.gen.test.js
Original file line number Diff line number Diff line change
@@ -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')

const argv = [
__dirname,
'--no-force-exit',
'-c',
'cli.gen.config.js'
]

const cmd = new NuxtCommand(generateCommand, argv)
await expect(cmd.run()).resolves.toBeUndefined()

expect(stdout.includes('Generated successfully')).toBe(true)
}, 80000)
expect(consola.log).toBeCalledWith('Generated successfully')
})
})

0 comments on commit 67d5601

Please sign in to comment.