Skip to content

Commit

Permalink
fix: updated fancy-mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 25, 2018
1 parent d78793e commit 8420ab2
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 40 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",
"bugs": "https://github.com/dxcli/dev-test/issues",
"dependencies": {
"fancy-mocha": "^0.3.1",
"fancy-mocha": "^0.3.2",
"lodash": "^4.17.4"
},
"devDependencies": {
Expand Down
6 changes: 2 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import run from '@dxcli/engine'
import {Plugin} from 'fancy-mocha'
import * as _ from 'lodash'

import {Options} from '.'

export default (async (next, __, args: string[] | string, opts = {}) => {
export default (args: string[] | string | undefined, opts: Options = {}) => async () => {
await run(_.castArray(args), {root: opts.root || module.parent!.parent!.filename})
await next({})
}) as Plugin<{}, (string[] | string), Partial<Options>>
}
12 changes: 7 additions & 5 deletions src/exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {Plugin} from 'fancy-mocha'
* @param code - expected code
* @default 0
*/
export default (async (next, __, code) => {
try {
await next({})
export default (code = 0) => {
const plugin = (() => {
throw new Error(`Expected hook to exit with code ${code} but it ran without exiting`)
} catch (err) {
}) as Plugin
plugin.catch = context => {
const err = context.error
if (!err['cli-ux'] || typeof err['cli-ux'].exit !== 'number') throw err
if (err['cli-ux'].exit !== code) {
throw new Error(`Expected hook to exit with ${code} but exited with ${err['cli-ux'].exit}`)
}
}
}) as Plugin<{}, number>
return plugin
}
6 changes: 2 additions & 4 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Engine} from '@dxcli/engine'
import {Plugin} from 'fancy-mocha'

import {Options} from './options'

Expand All @@ -15,9 +14,8 @@ import {Options} from './options'
* @param hookOpts - options to pass to hook. Config object will be passed automatically.
* @param opts - test options
*/
export default (async (next, __, event, hookOpts, opts = {}) => {
export default (event?: string, hookOpts: object = {}, opts: Options = {}) => async () => {
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
await engine.runHook(event!, hookOpts || {})
await next({})
}) as Plugin<{}, string, object, Partial<Options>>
}
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export interface Options {
* this is inferred automatically to the root of where dev-test is required
* it may need to be set if it needs to be pointed to a fixture instead
*/
root: string
root?: string
}
15 changes: 6 additions & 9 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ describe('command', () => {
test()
.stdout()
.command(['foo:bar'], {root})
.it('runs foo:bar', output => {
expect(output.stdout).to.equal('hello world!\n')
})
.run(output => expect(output.stdout).to.equal('hello world!\n'))
.end('runs foo:bar')

test()
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.it('runs foo:bar', output => {
expect(output.stdout).to.equal('hello foo!\n')
})
.run(output => expect(output.stdout).to.equal('hello foo!\n'))
.end('runs foo:bar')

test()
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.it('runs foo:bar', output => {
expect(output.stdout).to.equal('hello foo!\n')
})
.run(output => expect(output.stdout).to.equal('hello foo!\n'))
.end('runs foo:bar')
})
16 changes: 7 additions & 9 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ describe('hooks', () => {

test()
.stdout()
.exit(0)
.hook('init', {id: '-v'}, {root})
.it('catches -v', output => {
expect(output.stdout).to.equal(stdout)
})
.exit(0)
.run(output => expect(output.stdout).to.equal(stdout))
.end('catches -v')

test()
.stdout()
.exit(0)
.hook('init', {id: '--version'}, {root})
.it('catches --version', output => {
expect(output.stdout).to.equal(stdout)
})
.exit(0)
.run(output => expect(output.stdout).to.equal(stdout))
.end('catches --version')

test()
.stdout()
.hook('init', {}, {root})
.it('does not fail')
.end('does not fail')
})
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {expect, test} from '../src'
describe('stdout', () => {
test()
.stdout()
.it('logs', output => {
.end('logs', output => {
console.log('foo')
expect(output.stdout).to.equal('foo\n')
})

test()
.stdout()
.it('logs twice', output => {
.end('logs twice', output => {
console.log('foo')
expect(output.stdout).to.equal('foo\n')
console.log('bar')
Expand All @@ -26,7 +26,7 @@ describe('stdout + stderr', () => {
test()
.stdout()
.stderr()
.it('logs and errors', output => {
.end('logs and errors', output => {
console.log('foo')
console.error('bar')
expect(output.stdout).to.equal('foo\n')
Expand All @@ -39,7 +39,7 @@ os.forEach(os => {
describe(os, () => {
test()
.mock(OS, 'platform', () => os)
.it('sets os', () => {
.end('sets os', () => {
expect(OS.platform()).to.equal(os)
})
})
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1716,9 +1716,9 @@ extsprintf@^1.2.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"

fancy-mocha@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/fancy-mocha/-/fancy-mocha-0.3.1.tgz#2848e4644b3732c06392cfc2fdb2d4d5d4112423"
fancy-mocha@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/fancy-mocha/-/fancy-mocha-0.3.2.tgz#4bcc8dc4f323224ce0733bd1a093d8c485852406"
dependencies:
lodash "^4.17.4"
stdout-stderr "^0.1.4"
Expand Down

0 comments on commit 8420ab2

Please sign in to comment.