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')})
testHook('init')
})
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@
lodash "^4.17.4"
read-pkg "^3.0.0"

"@dxcli/config@^0.1.18":
version "0.1.18"
resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.18.tgz#2f8fed892f824a65e5dc5ac8d3fdce7c3150aee6"
dependencies:
debug "^3.1.0"
fs-extra "^5.0.0"
load-json-file "^4.0.0"
lodash "^4.17.4"
read-pkg "^3.0.0"

"@dxcli/dev-commitmsg@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@dxcli/dev-commitmsg/-/dev-commitmsg-0.0.3.tgz#8941971c9778b675dd14c407c1123b8bef397757"
Expand Down Expand Up @@ -183,12 +193,12 @@
read-pkg-up "^3.0.0"
supports-color "^5.1.0"

"@dxcli/engine@^0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@dxcli/engine/-/engine-0.0.1.tgz#7ea281102ff53186836488cded490e446a61ebc2"
"@dxcli/engine@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@dxcli/engine/-/engine-0.1.0.tgz#7006bae7e8e5267e8e6338671f1d6d5870e53fc6"
dependencies:
"@dxcli/command" "^0.1.9"
"@dxcli/config" "^0.1.17"
"@dxcli/config" "^0.1.18"
"@dxcli/loader" "^0.0.5"
cli-ux "^3.1.0"
rxjs "^5.5.6"
Expand Down

0 comments on commit 39bc877

Please sign in to comment.