mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
feat(cli): catch build errors with cli:buildError
hook (#6475)
This commit is contained in:
parent
a2be217876
commit
27e03531d6
@ -27,16 +27,25 @@ export default {
|
||||
},
|
||||
|
||||
async startDev (cmd, argv) {
|
||||
let nuxt
|
||||
try {
|
||||
const nuxt = await this._startDev(cmd, argv)
|
||||
|
||||
return nuxt
|
||||
nuxt = await this._listenDev(cmd, argv)
|
||||
} catch (error) {
|
||||
consola.fatal(error)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await this._buildDev(cmd, argv, nuxt)
|
||||
} catch (error) {
|
||||
await nuxt.callHook('cli:buildError', error)
|
||||
consola.error(error)
|
||||
}
|
||||
|
||||
return nuxt
|
||||
},
|
||||
|
||||
async _startDev (cmd, argv) {
|
||||
async _listenDev (cmd, argv) {
|
||||
const config = await cmd.getNuxtConfig({ dev: true, _build: true })
|
||||
const nuxt = await cmd.getNuxt(config)
|
||||
|
||||
@ -60,6 +69,11 @@ export default {
|
||||
await Promise.all(openerPromises)
|
||||
}
|
||||
|
||||
// Return instance
|
||||
return nuxt
|
||||
},
|
||||
|
||||
async _buildDev (cmd, argv, nuxt) {
|
||||
// Create builder instance
|
||||
const builder = await cmd.getBuilder(nuxt)
|
||||
|
||||
|
@ -41,7 +41,7 @@ describe('dev', () => {
|
||||
expect(consola.error).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('catches build error', async () => {
|
||||
test('catches build error and calls hook', async () => {
|
||||
const Nuxt = mockNuxt()
|
||||
const Builder = mockBuilder()
|
||||
|
||||
@ -55,6 +55,7 @@ describe('dev', () => {
|
||||
await Nuxt.fileRestartHook(builder)
|
||||
|
||||
expect(Nuxt.prototype.close).toHaveBeenCalled()
|
||||
expect(Nuxt.prototype.callHook).toHaveBeenCalledWith('cli:buildError', expect.any(Error))
|
||||
expect(consola.error).toHaveBeenCalledWith(new Error('Build Error'))
|
||||
})
|
||||
|
||||
@ -88,7 +89,7 @@ describe('dev', () => {
|
||||
builder.nuxt = new Nuxt()
|
||||
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)
|
||||
})
|
||||
|
||||
@ -104,7 +105,7 @@ describe('dev', () => {
|
||||
|
||||
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 () => {
|
||||
|
@ -83,6 +83,7 @@ export const mockNuxt = (implementation) => {
|
||||
}
|
||||
},
|
||||
options: {},
|
||||
callHook: jest.fn(),
|
||||
clearHook: jest.fn(),
|
||||
clearHooks: jest.fn(),
|
||||
close: jest.fn(),
|
||||
|
Loading…
Reference in New Issue
Block a user