Skip to content

Commit 5b214ad

Browse files
committed
fix: better exit error handling
1 parent 91ff2b5 commit 5b214ad

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/hook.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ export const testHook: TestHook = (
3434
const engine = new Engine()
3535
await engine.load(opts.root || module.parent!.parent!.filename)
3636
const run = () => engine.runHook(event, hookOpts)
37-
if (typeof opts.exit === 'number') await expect(run()).to.be.rejectedWith(`EEXIT: ${opts.exit}`)
38-
else await run()
37+
if (typeof opts.exit === 'number') {
38+
try {
39+
await run()
40+
throw new Error(`Expected hook to exit with code ${opts.exit} but it ran without exiting`)
41+
} catch (err) {
42+
if (!err['cli-ux'] || typeof err['cli-ux'].exit !== 'number') throw err
43+
if (err['cli-ux'].exit !== opts.exit) {
44+
throw new Error(`Expected hook to exit with ${opts.exit} but exited with ${err['cli-ux'].exit}`)
45+
}
46+
}
47+
} else await run()
3948
if (typeof opts.stdout === 'string') expect(output.stdout).to.equal(opts.stdout)
4049
if (typeof opts.stderr === 'string') expect(output.stderr).to.equal(opts.stderr)
4150
if (!fn) return

0 commit comments

Comments
 (0)