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'
|
2019-02-06 19:23:42 +00:00
|
|
|
import * as utils from '../../src/utils/'
|
2020-01-13 16:33:37 +00:00
|
|
|
import * as config from '../../src/utils/config'
|
2019-02-06 19:23:42 +00:00
|
|
|
import * as constants from '../../src/utils/constants'
|
2020-11-04 13:38:38 +00:00
|
|
|
import { consola, localPath } from '../utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
jest.mock('@nuxt/core')
|
|
|
|
jest.mock('@nuxt/builder')
|
|
|
|
jest.mock('@nuxt/generator')
|
|
|
|
|
2020-11-04 13:38:38 +00:00
|
|
|
jest.mock(localPath('@nuxt/core'))
|
|
|
|
jest.mock(localPath('@nuxt/builder'))
|
|
|
|
jest.mock(localPath('@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()
|
|
|
|
|
2020-05-18 08:21:15 +00:00
|
|
|
expect(minimistOptions.string.length).toBe(7)
|
|
|
|
expect(minimistOptions.boolean.length).toBe(6)
|
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-12-20 11:15:48 +00:00
|
|
|
const argv = ['-c', 'test-file', '-s', '-p', '3001']
|
|
|
|
const cmd = new Command({ options: { ...common, ...server } }, argv)
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
expect(cmd.argv['config-file']).toBe(argv[1])
|
|
|
|
expect(cmd.argv.spa).toBe(true)
|
|
|
|
expect(cmd.argv.universal).toBe(false)
|
|
|
|
expect(cmd.argv.port).toBe('3001')
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const cmd2 = new Command({ options: { ...common, ...server } }, ['--no-build'])
|
|
|
|
expect(cmd2.argv.build).toBe(false)
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
test('prints version automatically', async () => {
|
2019-02-06 19:23:42 +00:00
|
|
|
jest.spyOn(utils, 'forceExit').mockImplementation(() => {})
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const cmd = new Command({}, ['--version'])
|
2018-10-25 07:43:42 +00:00
|
|
|
cmd.showVersion = jest.fn()
|
2018-12-20 11:15:48 +00:00
|
|
|
await cmd.run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(cmd.showVersion).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
test('prints help automatically', async () => {
|
2019-02-06 19:23:42 +00:00
|
|
|
jest.spyOn(utils, 'forceExit').mockImplementation(() => {})
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const cmd = new Command({ options: allOptions }, ['-h'])
|
2018-10-25 07:43:42 +00:00
|
|
|
cmd.showHelp = jest.fn()
|
2018-12-20 11:15:48 +00:00
|
|
|
await cmd.run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(cmd.showHelp).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns nuxt config', async () => {
|
2020-01-13 16:33:37 +00:00
|
|
|
const loadConfigSpy = jest.spyOn(config, 'loadNuxtConfig')
|
|
|
|
|
|
|
|
const cmd = new Command({ name: 'test', options: allOptions }, ['-c', 'test-file', '-a', '-p', '3001', '-q', '-H'])
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const options = await cmd.getNuxtConfig({ testOption: true })
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(options.testOption).toBe(true)
|
|
|
|
expect(options.server.port).toBe(3001)
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith('Provided hostname argument has no value') // hostname check
|
2020-01-13 16:33:37 +00:00
|
|
|
expect(loadConfigSpy).toHaveBeenCalledTimes(1)
|
2020-05-18 08:21:15 +00:00
|
|
|
expect(loadConfigSpy).toHaveBeenCalledWith(expect.any(Object), {
|
|
|
|
command: 'test',
|
|
|
|
dev: false,
|
|
|
|
env: expect.objectContaining({
|
|
|
|
NODE_ENV: 'test'
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 16:33:37 +00:00
|
|
|
|
|
|
|
loadConfigSpy.mockRestore()
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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', () => {
|
2019-02-06 19:23:42 +00:00
|
|
|
jest.spyOn(constants, 'maxCharsPerLine').mockReturnValue(40)
|
|
|
|
|
2018-10-25 07:43:42 +00:00
|
|
|
const cmd = new Command({
|
2019-02-06 19:23:42 +00:00
|
|
|
description: 'a very long description that should wrap to the next line because is not longer ' +
|
2018-11-01 03:53:06 +00:00
|
|
|
'than the terminal width',
|
2018-10-25 07:43:42 +00:00
|
|
|
usage: 'this is how you do it',
|
2018-10-29 22:16:16 +00:00
|
|
|
options: {
|
|
|
|
...allOptions,
|
|
|
|
foo: {
|
|
|
|
type: 'boolean',
|
2019-02-06 19:23:42 +00:00
|
|
|
description: 'very long option that is longer than the terminal width and ' +
|
|
|
|
'should wrap to the next line'
|
2018-10-29 22:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
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(() => {})
|
|
|
|
const cmd = new Command()
|
|
|
|
cmd.showVersion()
|
|
|
|
expect(process.stdout.write).toHaveBeenCalled()
|
|
|
|
process.stdout.write.mockRestore()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('show help prints to stdout and exits', () => {
|
|
|
|
jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
|
|
|
|
const cmd = new Command()
|
|
|
|
cmd.showHelp()
|
|
|
|
expect(process.stdout.write).toHaveBeenCalled()
|
|
|
|
process.stdout.write.mockRestore()
|
|
|
|
})
|
2019-03-03 08:12:46 +00:00
|
|
|
|
|
|
|
test('can set and release lock', () => {
|
|
|
|
const release = jest.fn(() => Promise.resolve())
|
|
|
|
const cmd = new Command()
|
|
|
|
|
|
|
|
cmd.setLock(release)
|
|
|
|
cmd.releaseLock()
|
|
|
|
|
|
|
|
expect(release).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('logs warning when lock already exists and removes old lock', () => {
|
|
|
|
const release = jest.fn(() => Promise.resolve())
|
|
|
|
const cmd = new Command()
|
|
|
|
|
|
|
|
cmd.setLock(release)
|
|
|
|
cmd.setLock(release)
|
|
|
|
|
|
|
|
expect(consola.warn).toHaveBeenCalledTimes(1)
|
|
|
|
expect(consola.warn).toHaveBeenCalledWith(expect.stringMatching('A previous unreleased lock was found'))
|
|
|
|
expect(release).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
cmd.releaseLock()
|
|
|
|
expect(release).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|