feat(cli): catch build errors with cli:buildError hook (#6475)

This commit is contained in:
Pim 2019-09-29 10:11:32 +02:00 committed by Pooya Parsa
parent a2be217876
commit 27e03531d6
3 changed files with 23 additions and 7 deletions

View File

@ -27,16 +27,25 @@ export default {
}, },
async startDev (cmd, argv) { async startDev (cmd, argv) {
let nuxt
try { try {
const nuxt = await this._startDev(cmd, argv) nuxt = await this._listenDev(cmd, argv)
return nuxt
} catch (error) { } catch (error) {
consola.fatal(error)
return
}
try {
await this._buildDev(cmd, argv, nuxt)
} catch (error) {
await nuxt.callHook('cli:buildError', error)
consola.error(error) consola.error(error)
} }
return nuxt
}, },
async _startDev (cmd, argv) { async _listenDev (cmd, argv) {
const config = await cmd.getNuxtConfig({ dev: true, _build: true }) const config = await cmd.getNuxtConfig({ dev: true, _build: true })
const nuxt = await cmd.getNuxt(config) const nuxt = await cmd.getNuxt(config)
@ -60,6 +69,11 @@ export default {
await Promise.all(openerPromises) await Promise.all(openerPromises)
} }
// Return instance
return nuxt
},
async _buildDev (cmd, argv, nuxt) {
// Create builder instance // Create builder instance
const builder = await cmd.getBuilder(nuxt) const builder = await cmd.getBuilder(nuxt)

View File

@ -41,7 +41,7 @@ describe('dev', () => {
expect(consola.error).not.toHaveBeenCalled() expect(consola.error).not.toHaveBeenCalled()
}) })
test('catches build error', async () => { test('catches build error and calls hook', async () => {
const Nuxt = mockNuxt() const Nuxt = mockNuxt()
const Builder = mockBuilder() const Builder = mockBuilder()
@ -55,6 +55,7 @@ describe('dev', () => {
await Nuxt.fileRestartHook(builder) await Nuxt.fileRestartHook(builder)
expect(Nuxt.prototype.close).toHaveBeenCalled() expect(Nuxt.prototype.close).toHaveBeenCalled()
expect(Nuxt.prototype.callHook).toHaveBeenCalledWith('cli:buildError', expect.any(Error))
expect(consola.error).toHaveBeenCalledWith(new Error('Build Error')) expect(consola.error).toHaveBeenCalledWith(new Error('Build Error'))
}) })
@ -88,7 +89,7 @@ describe('dev', () => {
builder.nuxt = new Nuxt() builder.nuxt = new Nuxt()
await Nuxt.fileRestartHook(builder) await Nuxt.fileRestartHook(builder)
expect(consola.error).toHaveBeenCalledWith(new Error('Config Error')) expect(consola.fatal).toHaveBeenCalledWith(new Error('Config Error'))
// expect(Builder.prototype.watchRestart).toHaveBeenCalledTimes(1) // expect(Builder.prototype.watchRestart).toHaveBeenCalledTimes(1)
}) })
@ -104,7 +105,7 @@ describe('dev', () => {
await NuxtCommand.from(dev).run() await NuxtCommand.from(dev).run()
expect(consola.error).toHaveBeenCalledWith(new Error('Listen Error')) expect(consola.fatal).toHaveBeenCalledWith(new Error('Listen Error'))
}) })
test('dev doesnt force-exit by default', async () => { test('dev doesnt force-exit by default', async () => {

View File

@ -83,6 +83,7 @@ export const mockNuxt = (implementation) => {
} }
}, },
options: {}, options: {},
callHook: jest.fn(),
clearHook: jest.fn(), clearHook: jest.fn(),
clearHooks: jest.fn(), clearHooks: jest.fn(),
close: jest.fn(), close: jest.fn(),