test: fail tests in case of unhandled errors (#5255)

This commit is contained in:
Pooya Parsa 2019-03-16 16:12:35 +03:30 committed by GitHub
parent 2561b68ab1
commit d6b505aa50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 9 deletions

View File

@ -31,33 +31,47 @@ export default class NuxtCommand {
return new NuxtCommand(cmd, argv) return new NuxtCommand(cmd, argv)
} }
run() { async run() {
if (this.argv.help) { if (this.argv.help) {
this.showHelp() this.showHelp()
return Promise.resolve() return
} }
if (this.argv.version) { if (this.argv.version) {
this.showVersion() this.showVersion()
return Promise.resolve() return
} }
if (typeof this.cmd.run !== 'function') { 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) { if (this.argv.lock) {
runResolve.then(() => this.releaseLock()) await this.releaseLock()
} }
if (this.argv['force-exit']) { if (this.argv['force-exit']) {
const forceExitByUser = this.isUserSuppliedArg('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() { showVersion() {

View File

@ -141,7 +141,7 @@ describe('generate', () => {
mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] })) mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] }))
const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error']) 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 () => { test('do not throw an error when fail-on-error disabled and page errors', async () => {

View File

@ -1,6 +1,7 @@
import consola from 'consola' import consola from 'consola'
import chalk from 'chalk' import chalk from 'chalk'
import env from 'std-env' import env from 'std-env'
import exit from 'exit'
const isWin = env.windows const isWin = env.windows
@ -15,3 +16,11 @@ chalk.enabled = false
jest.setTimeout(60000) jest.setTimeout(60000)
consola.mockTypes(() => jest.fn()) consola.mockTypes(() => jest.fn())
function errorTrap(error) {
process.stderr.write('\n' + error.stack + '\n')
exit(1)
}
process.on('unhandledRejection', errorTrap)
process.on('uncaughtException', errorTrap)