Skip to content

Commit

Permalink
feat: added error to hook callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 20, 2018
1 parent d099f60 commit 036ec31
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface TestHookOptions {
root?: string
}

export type TestHookCallback<T> = (output: T) => Promise<void> | void
export type TestHookCallback<T> = (output: T & {error?: Error}) => Promise<void> | void

export interface TestHook {
(event: string, hookOpts: object, opts: TestHookOptions & {stdout: true, stderr: true}, fn: TestHookCallback<{stdout: string, stderr: string}>): void
Expand All @@ -34,11 +34,13 @@ export const testHook: TestHook = (
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
const run = () => engine.runHook(event, hookOpts)
let err
if (typeof opts.exit === 'number') {
try {
await run()
throw new Error(`Expected hook to exit with code ${opts.exit} but it ran without exiting`)
} catch (err) {
} catch (e) {
err = e
if (!err['cli-ux'] || typeof err['cli-ux'].exit !== 'number') throw err
if (err['cli-ux'].exit !== opts.exit) {
throw new Error(`Expected hook to exit with ${opts.exit} but exited with ${err['cli-ux'].exit}`)
Expand All @@ -49,6 +51,7 @@ export const testHook: TestHook = (
if (typeof opts.stderr === 'string') expect(output.stderr).to.equal(opts.stderr)
if (!fn) return
let o: any = {}
o.error = err
if (opts.stdout) o.stdout = output.stdout
if (opts.stderr) o.stderr = output.stderr
await fn(o)
Expand Down

0 comments on commit 036ec31

Please sign in to comment.