Skip to content

Commit

Permalink
test: fail tests in case of unhandled errors (#5255)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 16, 2019
1 parent 2561b68 commit d6b505a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
30 changes: 22 additions & 8 deletions packages/cli/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/unit/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
9 changes: 9 additions & 0 deletions test/utils/setup.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

0 comments on commit d6b505a

Please sign in to comment.