2018-10-29 22:16:16 +00:00
|
|
|
import { consola, mockGetNuxt, mockGetGenerator, NuxtCommand } from '../utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
import Command from '../../src/command'
|
|
|
|
|
|
|
|
describe('generate', () => {
|
|
|
|
let generate
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
generate = await import('../../src/commands/generate').then(m => m.default)
|
2018-10-25 07:43:42 +00:00
|
|
|
jest.spyOn(process, 'exit').mockImplementation(code => code)
|
|
|
|
})
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
afterAll(() => process.exit.mockRestore())
|
|
|
|
afterEach(() => jest.resetAllMocks())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
test('has run function', () => {
|
|
|
|
expect(typeof generate.run).toBe('function')
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('builds by default', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
const generator = mockGetGenerator(Promise.resolve())
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(generate).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(generator).toHaveBeenCalled()
|
|
|
|
expect(generator.mock.calls[0][0].build).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('doesnt build with no-build', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
const getArgv = Command.prototype.getArgv
|
|
|
|
Command.prototype.getArgv = jest.fn().mockImplementationOnce(() => {
|
|
|
|
return {
|
|
|
|
'_': ['.'],
|
|
|
|
rootDir: '.',
|
|
|
|
'config-file': 'nuxt.config.js',
|
|
|
|
build: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const generator = mockGetGenerator(Promise.resolve())
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(generate).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(generator).toHaveBeenCalled()
|
|
|
|
expect(generator.mock.calls[0][0].build).toBe(false)
|
|
|
|
Command.prototype.getArgv = getArgv
|
|
|
|
})
|
|
|
|
|
2018-11-17 23:05:51 +00:00
|
|
|
test('build with devtools', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
const generator = mockGetGenerator(Promise.resolve())
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate)
|
|
|
|
const args = ['generate', '.', '--devtools']
|
|
|
|
const argv = cmd.getArgv(args)
|
|
|
|
argv._ = ['.']
|
|
|
|
|
|
|
|
const options = await cmd.getNuxtConfig(argv)
|
|
|
|
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(options.vue.config.devtools).toBe(true)
|
|
|
|
expect(generator).toHaveBeenCalled()
|
|
|
|
expect(generator.mock.calls[0][0].build).toBe(true)
|
|
|
|
})
|
|
|
|
|
2018-11-22 15:48:26 +00:00
|
|
|
test('generate with modern mode', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetGenerator(Promise.resolve())
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate)
|
|
|
|
const args = ['generate', '.', '--m']
|
|
|
|
|
|
|
|
const options = await cmd.getNuxtConfig(cmd.getArgv(args))
|
|
|
|
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(options.modern).toBe('client')
|
|
|
|
})
|
|
|
|
|
2018-10-25 07:43:42 +00:00
|
|
|
test('catches error', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetGenerator(Promise.reject(new Error('Generator Error')))
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(generate).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith(new Error('Generator Error'))
|
|
|
|
})
|
|
|
|
})
|