2020-05-07 19:08:01 +00:00
|
|
|
import { MODES, TARGETS } from '@nuxt/utils'
|
2019-03-03 08:12:46 +00:00
|
|
|
import * as utils from '../../src/utils'
|
2018-12-20 11:15:48 +00:00
|
|
|
import { mockGetNuxt, mockGetBuilder, mockGetGenerator, NuxtCommand } from '../utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
describe('build', () => {
|
|
|
|
let build
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
build = await import('../../src/commands/build').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 build.run).toBe('function')
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('builds on universal mode', async () => {
|
|
|
|
mockGetNuxt({
|
2020-05-07 19:08:01 +00:00
|
|
|
mode: MODES.universal,
|
2018-10-25 07:43:42 +00:00
|
|
|
build: {
|
|
|
|
analyze: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const builder = mockGetBuilder(Promise.resolve())
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(build).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(builder).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('generates on spa mode', async () => {
|
|
|
|
mockGetNuxt({
|
2020-05-07 19:08:01 +00:00
|
|
|
mode: MODES.spa,
|
|
|
|
target: TARGETS.server,
|
2018-10-25 07:43:42 +00:00
|
|
|
build: {
|
|
|
|
analyze: false
|
|
|
|
}
|
|
|
|
})
|
2019-03-03 08:12:46 +00:00
|
|
|
const generate = mockGetGenerator()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(build).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(generate).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2018-11-17 23:05:51 +00:00
|
|
|
test('build with devtools', async () => {
|
|
|
|
mockGetNuxt({
|
2020-05-07 19:08:01 +00:00
|
|
|
mode: MODES.universal
|
2018-11-17 23:05:51 +00:00
|
|
|
})
|
|
|
|
const builder = mockGetBuilder(Promise.resolve())
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.', '--devtools'])
|
2018-11-17 23:05:51 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const options = await cmd.getNuxtConfig(cmd.argv)
|
2018-11-17 23:05:51 +00:00
|
|
|
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(options.vue.config.devtools).toBe(true)
|
|
|
|
expect(builder).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2018-11-22 15:48:26 +00:00
|
|
|
test('build with modern mode', async () => {
|
|
|
|
mockGetNuxt({
|
2020-05-07 19:08:01 +00:00
|
|
|
mode: MODES.universal
|
2018-11-22 15:48:26 +00:00
|
|
|
})
|
|
|
|
mockGetBuilder(Promise.resolve())
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.', '--m'])
|
2018-11-22 15:48:26 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
const options = await cmd.getNuxtConfig()
|
2018-11-22 15:48:26 +00:00
|
|
|
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(options.modern).toBe(true)
|
|
|
|
})
|
2019-02-08 10:06:47 +00:00
|
|
|
|
|
|
|
test('build force-exits by default', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetBuilder(Promise.resolve())
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledTimes(1)
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledWith('build', 5)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('build can set force exit explicitly', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetBuilder(Promise.resolve())
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.', '--force-exit'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledTimes(1)
|
|
|
|
expect(utils.forceExit).toHaveBeenCalledWith('build', false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('build can disable force exit explicitly', async () => {
|
|
|
|
mockGetNuxt()
|
|
|
|
mockGetBuilder(Promise.resolve())
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.', '--no-force-exit'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(utils.forceExit).not.toHaveBeenCalled()
|
|
|
|
})
|
2019-03-03 08:12:46 +00:00
|
|
|
|
|
|
|
test('build locks project by default', async () => {
|
|
|
|
mockGetNuxt({
|
2020-05-07 19:08:01 +00:00
|
|
|
mode: MODES.universal
|
2019-03-03 08:12:46 +00:00
|
|
|
})
|
|
|
|
mockGetBuilder(Promise.resolve())
|
|
|
|
|
|
|
|
const releaseLock = jest.fn(() => Promise.resolve())
|
|
|
|
const createLock = jest.fn(() => releaseLock)
|
|
|
|
jest.spyOn(utils, 'createLock').mockImplementation(createLock)
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(createLock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(releaseLock).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('build can disable locking', async () => {
|
|
|
|
mockGetNuxt({
|
2020-05-07 19:08:01 +00:00
|
|
|
mode: MODES.universal
|
2019-03-03 08:12:46 +00:00
|
|
|
})
|
|
|
|
mockGetBuilder(Promise.resolve())
|
|
|
|
|
|
|
|
const createLock = jest.fn(() => Promise.resolve())
|
|
|
|
jest.spyOn(utils, 'createLock').mockImplementationOnce(() => createLock)
|
|
|
|
|
|
|
|
const cmd = NuxtCommand.from(build, ['build', '.', '--no-lock'])
|
|
|
|
await cmd.run()
|
|
|
|
|
|
|
|
expect(createLock).not.toHaveBeenCalled()
|
|
|
|
})
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|