Skip to content

Commit

Permalink
feat: added testHook
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 20, 2018
1 parent e498629 commit 39bc877
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Jeff Dickey @jdxcode",
"dependencies": {
"@dxcli/dev-nyc-config": "^0.0.3",
"@dxcli/engine": "^0.0.1",
"@dxcli/engine": "^0.1.0",
"@types/ansi-styles": "^2.0.30",
"@types/chai": "^4.1.1",
"@types/chai-as-promised": "^7.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import run from '@dxcli/engine'

import {expect, it, output} from '.'

export interface RunCommandOptions {
export interface TestCommandOptions {
description?: string
stdout?: string
stderr?: string
exit?: number
root?: string
}

export const testCommand = (args: string[], opts: RunCommandOptions) => {
export const testCommand = (args: string[], opts: TestCommandOptions) => {
const description = opts.description || args[0]
let test = it
if (opts.stdout) test = test.stdout
Expand Down
28 changes: 28 additions & 0 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Engine} from '@dxcli/engine'
import {inspect} from 'util'

import {expect, it, output} from '.'

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

export const testHook = (event: string, hookOpts: object = {}, opts: TestHookOptions = {}) => {
const description = opts.description || `run hook with opts: ${inspect(hookOpts)}`
let test = it
if (opts.stdout) test = test.stdout
if (opts.stderr) test = test.stderr
test(description, async () => {
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
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)
})
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as _ from 'lodash'
import * as mocha from 'mocha'
export * from './command'
import stripAnsi = require('strip-ansi')

chai.use(chaiAsPromised)
Expand Down Expand Up @@ -138,3 +137,6 @@ export {
_it as it,
expect,
}

export * from './command'
export * from './hook'
2 changes: 2 additions & 0 deletions test/fixtures/multi/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "test",
"version": "0.0.0",
"private": true,
"dxcli": {
"commands": "./src/commands",
Expand Down
10 changes: 10 additions & 0 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as path from 'path'

import {describe, 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')})
</