Skip to content

Commit

Permalink
feat: use fancy-mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 25, 2018
1 parent 49c1ddf commit 89d55b1
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 644 deletions.
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,33 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/dxcli/dev-test/issues",
"dependencies": {
"debug": "^3.1.0",
"lodash": "^4.17.4",
"strip-ansi": "^4.0.0"
"fancy-mocha": "^0.1.0",
"lodash": "^4.17.4"
},
"devDependencies": {
"@dxcli/dev-nyc-config": "^0.0.3",
"@dxcli/dev-semantic-release": "^0.1.0",
"@dxcli/dev-test": "^0.7.0",
"@dxcli/dev-tslint": "^0.0.15",
"@dxcli/engine": "^0.1.5",
"@dxcli/version": "^0.1.4",
"@types/ansi-styles": "^2.0.30",
"@types/chai": "^4.1.1",
"@types/chai": "^4.1.2",
"@types/chai-as-promised": "^7.1.0",
"@types/lodash": "^4.14.93",
"@types/mocha": "^2.2.46",
"@types/lodash": "^4.14.96",
"@types/mocha": "^2.2.47",
"@types/node": "^9.3.0",
"@types/read-pkg": "^3.0.0",
"@types/strip-ansi": "^3.0.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cli-ux": "^3.1.5",
"esdoc": "^1.0.4",
"esdoc-standard-plugin": "^1.0.0",
"esdoc-undocumented-identifier-plugin": "^1.0.0",
"eslint": "^4.16.0",
"eslint-config-dxcli": "^1.1.4",
"fancy-mocha": "^0.0.6",
"husky": "^0.14.3",
"mocha": "^5.0.0",
"mocha-junit-reporter": "^1.16.0",
"nps": "^5.7.1",
"nps-utils": "^1.5.0",
"nyc": "^11.4.1",
"ts-node": "^4.1.0",
"typedoc": "^0.9.0",
"typescript": "^2.6.2"
},
"engines": {
Expand Down
25 changes: 8 additions & 17 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import run from '@dxcli/engine'
import {Plugin} from 'fancy-mocha'
import * as _ from 'lodash'

import {Options} from '.'

export default (args: string[] | string, opts: Options = {}) => {
return {
async before() {
args = _.castArray(args)
await run(args, {root: opts.root || module.parent!.parent!.filename})
}
}
}
// const exit = opts.exit || 0
// try {
// await run(args, {root: opts.root || module.parent!.parent!.filename})
// } catch (err) {
// if (err.code !== 'EEXIT') throw err
// if (err['cli-ux'].exit !== exit) {
// throw new Error(`Expected exit code to be ${exit} but got ${err['cli-ux'].exit}`)
// }
// }
export default (async (next, __, args: string[] | string, opts: Options) => {
_.defaults(opts, {
root: module.parent!.parent!.filename
})
await run(_.castArray(args), {root: opts.root})
await next({})
}) as Plugin<{}, (string[] | string), Partial<Options>>
24 changes: 10 additions & 14 deletions src/exit.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import {CLIError} from 'cli-ux'
import {Plugin} from 'fancy-mocha'

/**
* ensures that a dxcli command or hook exits
*
* @param code - expected code
* @default 0
*/
export default (code = 0) => {
let err: CLIError
return {
catch(_: any, e: CLIError) {
err = e
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}`)
}
},
finally() {
if (!err) throw new Error(`Expected hook to exit with code ${code} but it ran without exiting`)
export default (async (next, __, code) => {
try {
await next({})
throw new Error(`Expected hook to exit with code ${code} but it ran without exiting`)
} catch (err) {
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>
21 changes: 11 additions & 10 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Engine} from '@dxcli/engine'
import {Plugin} from 'fancy-mocha'
import * as _ from 'lodash'

import {Options} from './options'

Expand All @@ -14,13 +16,12 @@ import {Options} from './options'
* @param hookOpts - options to pass to hook. Config object will be passed automatically.
* @param opts - test options
*/
export default (event: string, hookOpts: object = {}, opts: Options = {}) => {
return {
async before() {
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
const run = () => engine.runHook(event, hookOpts)
await run()
}
}
}
export default (async (next, __, event, hookOpts, opts: Options) => {
_.defaults(opts, {
root: module.parent!.parent!.filename
})
const engine = new Engine()
await engine.load(opts.root)
await engine.runHook(event!, hookOpts || {})
await next({})
}) as Plugin<{}, string, object, Partial<Options>>
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import base, {expect, Test, TestBase} from 'fancy-mocha'
import {Base, expect, fancy} from 'fancy-mocha'

import command from './command'
import exit from './exit'
import hook from './hook'

import {Options} from './options'

export const test = base
.extend('command', command)
.extend('exit', exit)
.extend('hook', hook)
export const test = fancy
.register('command', command)
.register('exit', exit)
.register('hook', hook)

export default test

export {
expect,
exit,
Test,
TestBase,
Options,
Base,
}
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
}
36 changes: 18 additions & 18 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import test, {expect} from '../src'
const root = path.join(__dirname, 'fixtures/multi')

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

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

test
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.it('runs foo:bar', output => {
expect(output.stdout).to.equal('hello foo!\n')
})
test()
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.it('runs foo:bar', output => {
expect(output.stdout).to.equal('hello foo!\n')
})
})
6 changes: 3 additions & 3 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ describe('hooks', () => {
const stdout = `test/0.0.0 (${process.platform}-${process.arch}) node-${process.version}\n`
const root = path.join(__dirname, 'fixtures/multi')

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

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

test
test()
.stdout()
.hook('init', {}, {root})
.it('does not fail')
Expand Down
16 changes: 8 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import * as OS from 'os'
import {expect, test} from '../src'

describe('stdout', () => {
test
test()
.stdout()
.it('logs', output => {
console.log('foo')
expect(output.stdout).to.equal('foo\n')
})

test
test()
.stdout()
.it('logs twice', output => {
console.log('foo')
Expand All @@ -23,7 +23,7 @@ describe('stdout', () => {
})

describe('stdout + stderr', () => {
test
test()
.stdout()
.stderr()
.it('logs and errors', output => {
Expand All @@ -37,7 +37,7 @@ describe('stdout + stderr', () => {
const os = ['darwin', 'win32', 'linux']
os.forEach(os => {
describe(os, () => {
test
test()
.mock(OS, 'platform', () => os)
.it('sets os', () => {
expect(OS.platform()).to.equal(os)
Expand All @@ -46,20 +46,20 @@ os.forEach(os => {
})

describe('mock env', () => {
test
test()
.env({foo: 'bar'})
.it('gets env', () => {
expect(process.env).to.deep.equal({foo: 'bar'})
})

test
test()
.env({foo: 'baz'})
.it('gets env from it', () => {
expect(process.env).to.deep.equal({foo: 'baz'})
})

test
.env()
test()
.env({})
.it('clears env', () => {
expect(process.env).to.deep.equal({})
})
Expand Down
Loading

0 comments on commit 89d55b1

Please sign in to comment.