Skip to content

Commit

Permalink
feat: added testHook callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 20, 2018
1 parent 5a94f8e commit e5f9a87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ import {expect, it, output} from '.'

export interface TestHookOptions {
description?: string
stdout?: string
stderr?: string
stdout?: string | boolean
stderr?: string | boolean
exit?: number
root?: string
}

export const testHook = (event: string, hookOpts: object = {}, opts: TestHookOptions = {}) => {
export type TestHookCallback<T> = (output: T) => Promise<void> | void

export interface TestHook {
(event: string, hookOpts: object, opts: TestHookOptions & {stdout: true, stderr: true}, fn: TestHookCallback<{stdout: string, stderr: string}>): void
(event: string, hookOpts: object, opts: TestHookOptions & {stdout: true}, fn: TestHookCallback<{stdout: string}>): void
(event: string, hookOpts: object, opts: TestHookOptions & {stderr: true}, fn: TestHookCallback<{stderr: string}>): void
(event: string, hookOpts?: object, opts?: TestHookOptions, fn?: TestHookCallback<{}>): void
}

export const testHook: TestHook = (
event: string,
hookOpts: object = {},
opts: TestHookOptions = {},
fn?: TestHookCallback<any>,
) => {
const description = opts.description || `run hook with opts: ${inspect(hookOpts)}`
let test = it
if (opts.stdout) test = test.stdout
Expand All @@ -22,7 +36,12 @@ export const testHook = (event: string, hookOpts: object = {}, opts: TestHookOpt
const run = () => engine.runHook(event, hookOpts)
if (typeof opts.exit === 'number') await expect(run()).to.be.rejectedWith(`EEXIT: ${opts.exit}`)
else await run()
if (opts.stdout) expect(output.stdout).to.equal(opts.stdout)
if (opts.stderr) expect(output.stderr).to.equal(opts.stderr)
if (typeof opts.stdout === 'string') expect(output.stdout).to.equal(opts.stdout)
if (typeof opts.stderr === 'string') expect(output.stderr).to.equal(opts.stderr)
if (!fn) return
let o: any = {}
if (opts.stdout) o.stdout = output.stdout
if (opts.stderr) o.stderr = output.stderr
await fn(o)
})
}
5 changes: 4 additions & 1 deletion test/hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as path from 'path'

import {describe, testHook} from '../src'
import {describe, expect, testHook} from '../src'

describe('hooks', () => {
const stdout = `test/0.0.0 (${process.platform}-${process.arch}) node-${process.version}\n`
testHook('init', {id: '-v'}, {stdout, exit: 0, root: path.join(__dirname, 'fixtures/multi')})
testHook('init', {id: '--version'}, {stdout, exit: 0, root: path.join(__dirname, 'fixtures/multi')})
testHook('init')
testHook('init', {id: '--version'}, {stdout: true, exit: 0, root: path.join(__dirname, 'fixtures/multi')}, output => {
expect(output.stdout).to.equal(stdout)
})
})

0 comments on commit e5f9a87

Please sign in to comment.