2019-03-03 08:12:46 +00:00
|
|
|
import * as utils from '../../src/utils'
|
2018-12-20 11:15:48 +00:00
|
|
|
import { mockGetNuxt, mockGetGenerator, NuxtCommand } from '../utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
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)
|
2019-02-06 19:23:42 +00:00
|
|
|
jest.spyOn(utils, 'forceExit').mockImplementation(() => {})
|
2019-03-03 08:12:46 +00:00
|
|
|
jest.spyOn(utils, 'createLock').mockImplementation(() => () => {})
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
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()
|
2019-03-03 08:12:46 +00:00
|
|
|
const generator = mockGetGenerator()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
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()
|
2019-03-03 08:12:46 +00:00
|
|
|
const generator = mockGetGenerator()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
await NuxtCommand.run(generate, ['generate', '.', '--no-build'])
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(generator).toHaveBeenCalled()
|
|
|
|
expect(generator.mock.calls[0][0].build).toBe(false)
|
|
|
|
})
|
|
|
|
|
2018-11-17 23:05:51 +00:00
|
|
|
test('build with devtools', async () => {
|
|
|
|
mockGetNuxt()
|
2019-03-03 08:12:46 +00:00
|
|
|
const generator = mockGetGenerator()
|
2018-11-17 23:05:51 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--devtools'])
|
2018-11-17 23:05:51 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const options = await cmd.getNuxtConfig()
|
2018-11-17 23:05:51 +00:00
|
|
|
|
|
|
|
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()
|
2019-03-03 08:12:46 +00:00
|
|
|
mockGetGenerator()
|
2019-02-08 10:06:47 +00:00
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--m'])
|
|
|
|
|
|
|
|
const options = await cmd.getNuxtConfig()
|
|
|
|
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(options.modern).toBe('client')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('generate force-exits by default', async () => {
|
|
|
|
mockGetNuxt()
|
2019-03-03 08:12:46 +00:00
|
|
|
mockGetGenerator()
|
2019-02-08 10:06:47 +00:00
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledTimes(1)
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledWith('generate', 5)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('generate can set force exit explicitly', async () => {
|
|
|
|
mockGetNuxt()
|
2019-03-03 08:12:46 +00:00
|
|
|
mockGetGenerator()
|
2019-02-08 10:06:47 +00:00
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--force-exit'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledTimes(1)
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledWith('generate', false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('generate can disable force exit explicitly', async () => {
|
|
|
|
mockGetNuxt()
|
2019-03-03 08:12:46 +00:00
|
|
|
mockGetGenerator()
|
2019-02-08 10:06:47 +00:00
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--no-force-exit'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(utils.forceExit).not.toHaveBeenCalled()
|
|
|
|
})
|
2019-03-03 08:12:46 +00:00
|
|
|
|
|
|
|
test('generate locks project by default twice', async () => {
|
|
|
|
const releaseLock = jest.fn(() => Promise.resolve())
|
|
|
|
const createLock = jest.fn(() => releaseLock)
|
|
|
|
jest.spyOn(utils, 'createLock').mockImplementation(createLock)
|
|
|
|
|
|
|
|
let buildDone
|
|
|
|
mockGetNuxt({ generate: {} }, {
|
|
|
|
hook: (hookName, fn) => (buildDone = fn)
|
|
|
|
})
|
|
|
|
|
|
|
|
mockGetGenerator(async () => {
|
|
|
|
await buildDone()
|
|
|
|
})
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(createLock).toHaveBeenCalledTimes(2)
|
|
|
|
expect(releaseLock).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('generate can disable locking', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetGenerator()
|
|
|
|
|
|
|
|
const createLock = jest.fn(() => Promise.resolve())
|
|
|
|
jest.spyOn(utils, 'createLock').mockImplementationOnce(() => createLock)
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--no-lock'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(createLock).not.toHaveBeenCalled()
|
|
|
|
})
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|