mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
test: fail tests in case of unhandled errors (#5255)
This commit is contained in:
parent
2561b68ab1
commit
d6b505aa50
@ -31,33 +31,47 @@ export default class NuxtCommand {
|
||||
return new NuxtCommand(cmd, argv)
|
||||
}
|
||||
|
||||
run() {
|
||||
async run() {
|
||||
if (this.argv.help) {
|
||||
this.showHelp()
|
||||
return Promise.resolve()
|
||||
return
|
||||
}
|
||||
|
||||
if (this.argv.version) {
|
||||
this.showVersion()
|
||||
return Promise.resolve()
|
||||
return
|
||||
}
|
||||
|
||||
if (typeof this.cmd.run !== 'function') {
|
||||
return Promise.resolve()
|
||||
return
|
||||
}
|
||||
|
||||
const runResolve = Promise.resolve(this.cmd.run(this))
|
||||
let cmdError
|
||||
|
||||
try {
|
||||
await this.cmd.run(this)
|
||||
} catch (e) {
|
||||
cmdError = e
|
||||
}
|
||||
|
||||
if (this.argv.lock) {
|
||||
runResolve.then(() => this.releaseLock())
|
||||
await this.releaseLock()
|
||||
}
|
||||
|
||||
if (this.argv['force-exit']) {
|
||||
const forceExitByUser = this.isUserSuppliedArg('force-exit')
|
||||
runResolve.then(() => forceExit(this.cmd.name, forceExitByUser ? false : forceExitTimeout))
|
||||
if (cmdError) {
|
||||
consola.fatal(cmdError)
|
||||
}
|
||||
forceExit(this.cmd.name, forceExitByUser ? false : forceExitTimeout)
|
||||
if (forceExitByUser) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return runResolve
|
||||
if (cmdError) {
|
||||
throw cmdError
|
||||
}
|
||||
}
|
||||
|
||||
showVersion() {
|
||||
|
@ -141,7 +141,7 @@ describe('generate', () => {
|
||||
mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] }))
|
||||
|
||||
const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error'])
|
||||
await expect(cmd.run()).rejects
|
||||
await expect(cmd.run()).rejects.toThrow('Error generating pages, exiting with non-zero code')
|
||||
})
|
||||
|
||||
test('do not throw an error when fail-on-error disabled and page errors', async () => {
|
||||
|
@ -1,6 +1,7 @@
|
||||
import consola from 'consola'
|
||||
import chalk from 'chalk'
|
||||
import env from 'std-env'
|
||||
import exit from 'exit'
|
||||
|
||||
const isWin = env.windows
|
||||
|
||||
@ -15,3 +16,11 @@ chalk.enabled = false
|
||||
jest.setTimeout(60000)
|
||||
|
||||
consola.mockTypes(() => jest.fn())
|
||||
|
||||
function errorTrap(error) {
|
||||
process.stderr.write('\n' + error.stack + '\n')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
process.on('unhandledRejection', errorTrap)
|
||||
process.on('uncaughtException', errorTrap)
|
||||
|
Loading…
Reference in New Issue
Block a user