2018-10-25 07:43:42 +00:00
|
|
|
import Command from '../../src/command'
|
2018-10-29 22:16:16 +00:00
|
|
|
import { common, server } from '../../src/options'
|
2018-10-25 15:40:55 +00:00
|
|
|
import { consola } from '../utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
jest.mock('@nuxt/core')
|
|
|
|
jest.mock('@nuxt/builder')
|
|
|
|
jest.mock('@nuxt/generator')
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
const allOptions = {
|
|
|
|
...common,
|
|
|
|
...server
|
|
|
|
}
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
describe('cli/command', () => {
|
|
|
|
beforeEach(() => jest.restoreAllMocks())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
test('builds minimist options', () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
const cmd = new Command({ options: allOptions })
|
2018-10-25 07:43:42 +00:00
|
|
|
const minimistOptions = cmd._getMinimistOptions()
|
|
|
|
|
|
|
|
expect(minimistOptions.string.length).toBe(4)
|
2018-10-29 22:16:16 +00:00
|
|
|
expect(minimistOptions.boolean.length).toBe(4)
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(minimistOptions.alias.c).toBe('config-file')
|
2018-10-29 22:16:16 +00:00
|
|
|
expect(minimistOptions.default.c).toBe(common['config-file'].default)
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('parses args', () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
const cmd = new Command({ options: { ...common, ...server } })
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
let args = ['-c', 'test-file', '-s', '-p', '3001']
|
|
|
|
let argv = cmd.getArgv(args)
|
|
|
|
|
|
|
|
expect(argv['config-file']).toBe(args[1])
|
|
|
|
expect(argv.spa).toBe(true)
|
|
|
|
expect(argv.universal).toBe(false)
|
|
|
|
expect(argv.port).toBe('3001')
|
|
|
|
|
|
|
|
args = ['--no-build']
|
|
|
|
argv = cmd.getArgv(args)
|
|
|
|
|
|
|
|
expect(argv.build).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('prints version automatically', () => {
|
|
|
|
const cmd = new Command()
|
|
|
|
cmd.showVersion = jest.fn()
|
|
|
|
|
|
|
|
const args = ['--version']
|
|
|
|
cmd.getArgv(args)
|
|
|
|
|
|
|
|
expect(cmd.showVersion).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('prints help automatically', () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
const cmd = new Command({ options: allOptions })
|
2018-10-25 07:43:42 +00:00
|
|
|
cmd.showHelp = jest.fn()
|
|
|
|
|
|
|
|
const args = ['-h']
|
|
|
|
cmd.getArgv(args)
|
|
|
|
|
|
|
|
expect(cmd.showHelp).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns nuxt config', async () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
const cmd = new Command({ options: allOptions })
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
const args = ['-c', 'test-file', '-a', '-p', '3001', '-q', '-H']
|
|
|
|
const argv = cmd.getArgv(args)
|
|
|
|
argv._ = ['.']
|
|
|
|
|
|
|
|
const options = await cmd.getNuxtConfig(argv, { testOption: true })
|
|
|
|
|
|
|
|
expect(options.testOption).toBe(true)
|
|
|
|
expect(options.server.port).toBe(3001)
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith('Provided hostname argument has no value') // hostname check
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns Nuxt instance', async () => {
|
|
|
|
const cmd = new Command()
|
|
|
|
const nuxt = await cmd.getNuxt()
|
|
|
|
|
|
|
|
expect(nuxt.constructor.name).toBe('Nuxt')
|
|
|
|
expect(typeof nuxt.ready).toBe('function')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns Builder instance', async () => {
|
|
|
|
const cmd = new Command()
|
|
|
|
const builder = await cmd.getBuilder()
|
|
|
|
|
|
|
|
expect(builder.constructor.name).toBe('Builder')
|
|
|
|
expect(typeof builder.build).toBe('function')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns Generator instance', async () => {
|
|
|
|
const cmd = new Command()
|
|
|
|
const generator = await cmd.getGenerator()
|
|
|
|
|
|
|
|
expect(generator.constructor.name).toBe('Generator')
|
|
|
|
expect(typeof generator.generate).toBe('function')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('builds help text', () => {
|
|
|
|
const cmd = new Command({
|
|
|
|
description: 'a very long description that is longer than 80 chars and ' +
|
|
|
|
'should wrap to the next line while keeping indentation',
|
|
|
|
usage: 'this is how you do it',
|
2018-10-29 22:16:16 +00:00
|
|
|
options: {
|
|
|
|
...allOptions,
|
|
|
|
foo: {
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'very long option that is longer than 80 chars and ' +
|
|
|
|
'should wrap to the next line while keeping indentation'
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
expect(cmd._getHelp()).toMatchSnapshot()
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('show version prints to stdout and exits', () => {
|
|
|
|
jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
|
|
|
|
jest.spyOn(process, 'exit').mockImplementationOnce(code => code)
|
|
|
|
|
|
|
|
const cmd = new Command()
|
|
|
|
cmd.showVersion()
|
|
|
|
|
|
|
|
expect(process.stdout.write).toHaveBeenCalled()
|
|
|
|
expect(process.exit).toHaveBeenCalled()
|
|
|
|
|
|
|
|
process.stdout.write.mockRestore()
|
|
|
|
process.exit.mockRestore()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('show help prints to stdout and exits', () => {
|
|
|
|
jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
|
|
|
|
jest.spyOn(process, 'exit').mockImplementationOnce(code => code)
|
|
|
|
|
|
|
|
const cmd = new Command()
|
|
|
|
cmd.showHelp()
|
|
|
|
|
|
|
|
expect(process.stdout.write).toHaveBeenCalled()
|
|
|
|
expect(process.exit).toHaveBeenCalled()
|
|
|
|
|
|
|
|
process.stdout.write.mockRestore()
|
|
|
|
process.exit.mockRestore()
|
|
|
|
})
|
|
|
|
})
|