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
|
|
|
})
|
|
|
|
|
2019-03-04 20:12:33 +00:00
|
|
|
afterAll(() => {
|
|
|
|
process.exit.mockRestore()
|
|
|
|
})
|
|
|
|
|
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()
|
2019-03-11 19:55:54 +00:00
|
|
|
return { errors: [] }
|
2019-03-03 08:12:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
|
|
|
})
|
2019-03-11 19:55:54 +00:00
|
|
|
|
|
|
|
test('throw an error when fail-on-error enabled and page errors', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] }))
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error'])
|
2019-03-16 12:42:35 +00:00
|
|
|
await expect(cmd.run()).rejects.toThrow('Error generating pages, exiting with non-zero code')
|
2019-03-11 19:55:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('do not throw an error when fail-on-error disabled and page errors', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] }))
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.'])
|
|
|
|
await cmd.run()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('do not throw an error when fail-on-error enabled and no page errors', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetGenerator()
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error'])
|
|
|
|
await cmd.run()
|
|
|
|
})
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|