Skip to content

Commit

Permalink
fix: updated fancy-test
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 26, 2018
1 parent 1dcc28b commit 39078f0
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 74 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "/~https://github.com/dxcli/dev-test/issues",
"dependencies": {
"fancy-mocha": "^0.3.2",
"fancy-test": "^0.3.5",
"lodash": "^4.17.4"
},
"devDependencies": {
"@dxcli/dev-nyc-config": "^0.0.3",
"@dxcli/dev-semantic-release": "^0.1.0",
"@dxcli/dev-tslint": "^0.0.15",
"@dxcli/engine": "^0.1.5",
"@dxcli/version": "^0.1.4",
"@dxcli/engine": "^0.1.7",
"@dxcli/version": "^0.1.5",
"@types/chai": "^4.1.2",
"@types/chai-as-promised": "^7.1.0",
"@types/lodash": "^4.14.96",
Expand All @@ -28,7 +28,7 @@
"eslint-config-dxcli": "^1.1.4",
"husky": "^0.14.3",
"mocha": "^5.0.0",
"mocha-junit-reporter": "^1.16.0",
"mocha-junit-reporter": "^1.17.0",
"nps": "^5.7.1",
"nps-utils": "^1.5.0",
"nyc": "^11.4.1",
Expand Down
10 changes: 7 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as _ from 'lodash'

import {Options} from '.'

export default (args: string[] | string | undefined, opts: Options = {}) => async () => {
await run(_.castArray(args), {root: opts.root || module.parent!.parent!.filename})
}
export default (args: string[] | string | undefined, opts: Options = {}) => ({
async run(ctx: {expectation: string}) {
args = _.castArray(args)
ctx.expectation = ctx.expectation || `runs ${args.join(' ')}`
await run(args, {root: opts.root || module.parent!.parent!.filename})
}
})
15 changes: 6 additions & 9 deletions src/exit.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import {Plugin} from 'fancy-mocha'

/**
* ensures that a dxcli command or hook exits
*
* @param code - expected code
* @default 0
*/
export default (code = 0) => {
const plugin = (() => {
export default (code = 0) => ({
run() {
throw new Error(`Expected hook to exit with code ${code} but it ran without exiting`)
}) as Plugin
plugin.catch = context => {
const err = context.error
},
catch(ctx: {error: any}) {
const err = ctx.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}`)
}
}
return plugin
}
})
13 changes: 8 additions & 5 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ 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 = {}) => async () => {
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
await engine.runHook(event!, hookOpts || {})
}
export default (event?: string, hookOpts: object = {}, opts: Options = {}) => ({
async run(ctx: {expectation: string}) {
ctx.expectation = ctx.expectation || `runs ${event} hook`
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
await engine.runHook(event!, hookOpts || {})
}
})
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Base, CatchOptions, EnvOptions, expect, fancy, NockScope, StdmockOptions} from 'fancy-mocha'
import {Context, EnvOptions, expect, fancy, NockScope, Plugin} from 'fancy-test'

import command from './command'
import exit from './exit'
Expand All @@ -14,11 +14,10 @@ export const test = fancy
export default test

export {
Base,
CatchOptions,
Context,
EnvOptions,
NockScope,
Options,
StdmockOptions,
Plugin,
expect,
}
20 changes: 10 additions & 10 deletions test/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import * as path from 'path'

import test, {expect} from '../src'
import fancy, {expect} from '../src'

const root = path.join(__dirname, 'fixtures/multi')

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

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

test()
fancy
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.run(output => expect(output.stdout).to.equal('hello foo!\n'))
.end('runs foo:bar')
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
.it()
})
18 changes: 9 additions & 9 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import * as path from 'path'

import {expect, test} from '../src'
import fancy, {expect} from '../src'

describe('hooks', () => {
const stdout = `test/0.0.0 (${process.platform}-${process.arch}) node-${process.version}\n`
const root = path.join(__dirname, 'fixtures/multi')

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

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

test()
fancy
.stdout()
.hook('init', {}, {root})
.end('does not fail')
.it()
})
10 changes: 5 additions & 5 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()
.end('logs', output => {
console.log('foo')
expect(output.stdout).to.equal('foo\n')
})

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

describe('stdout + stderr', () => {
test()
test
.stdout()
.stderr()
.end('logs and errors', output => {
Expand All @@ -37,8 +37,8 @@ describe('stdout + stderr', () => {
const os = ['darwin', 'win32', 'linux']
os.forEach(os => {
describe(os, () => {
test()
.mock(OS, 'platform', () => os)
test
.stub(OS, 'platform', () => os)
.end('sets os', () => {
expect(OS.platform()).to.equal(os)
})
Expand Down
78 changes: 53 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
dependencies:
find-up "^2.1.0"

"@dxcli/command@^0.1.11", "@dxcli/command@^0.1.13":
"@dxcli/command@^0.1.13":
version "0.1.13"
resolved "https://registry.yarnpkg.com/@dxcli/command/-/command-0.1.13.tgz#b4dec2e629076c2b50f4cf43c9324dd6ccb98382"
dependencies:
Expand All @@ -112,6 +112,15 @@
cli-ux "^3.1.3"
debug "^3.1.0"

"@dxcli/command@^0.1.15":
version "0.1.15"
resolved "https://registry.yarnpkg.com/@dxcli/command/-/command-0.1.15.tgz#872e6f186f09f074a2195ec3dd475d8a4947a611"
dependencies:
"@dxcli/config" "^0.1.24"
"@dxcli/parser" "^0.0.4"
cli-ux "^3.1.5"
debug "^3.1.0"

"@dxcli/config@^0.1.20":
version "0.1.21"
resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.21.tgz#d3cf65e672c5d0e36ba64982199021ede4b7a11a"
Expand All @@ -122,6 +131,16 @@
lodash "^4.17.4"
read-pkg "^3.0.0"

"@dxcli/config@^0.1.22", "@dxcli/config@^0.1.24":
version "0.1.24"
resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.24.tgz#fc6d14f7c91b742bf3d0ca7c2974565a959471ae"
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.4":
version "0.0.4"
resolved "https://registry.yarnpkg.com/@dxcli/dev-commitmsg/-/dev-commitmsg-0.0.4.tgz#c26f6b5856aa50d91dd36adc18d0c48f3d0dfa61"
Expand Down Expand Up @@ -153,23 +172,23 @@
tslint "^5.9.1"
tslint-xo "^0.5.0"

"@dxcli/engine@^0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@dxcli/engine/-/engine-0.1.5.tgz#7d576b0d15cd65a4f57035af2bead7c0c069f0c2"
"@dxcli/engine@^0.1.7":
version "0.1.7"
resolved "https://registry.yarnpkg.com/@dxcli/engine/-/engine-0.1.7.tgz#8ca419f6d0e2a9b52f916946451504a93b388959"
dependencies:
"@dxcli/command" "^0.1.13"
"@dxcli/config" "^0.1.20"
"@dxcli/loader" "^0.1.4"
cli-ux "^3.1.3"
"@dxcli/command" "^0.1.15"
"@dxcli/config" "^0.1.24"
"@dxcli/loader" "^0.1.6"
cli-ux "^3.1.5"
debug "^3.1.0"

"@dxcli/loader@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@dxcli/loader/-/loader-0.1.4.tgz#ac8e3e02574f191158beb9c7f1661229ebe52910"
"@dxcli/loader@^0.1.6":
version "0.1.6"
resolved "https://registry.yarnpkg.com/@dxcli/loader/-/loader-0.1.6.tgz#b0e1cb1bf12ca4f4252cb529b5a6dd18f2ed66bd"
dependencies:
"@dxcli/config" "^0.1.20"
"@dxcli/config" "^0.1.24"
"@dxcli/manifest-file" "^0.0.3"
cli-ux "^3.1.3"
cli-ux "^3.1.5"
debug "^3.1.0"
globby "^7.1.1"
lodash "^4.17.4"
Expand All @@ -192,6 +211,14 @@
chalk "^2.3.0"
lodash "^4.17.4"

"@dxcli/parser@^0.0.4":
version "0.0.4"
resolved "https://registry.yarnpkg.com/@dxcli/parser/-/parser-0.0.4.tgz#dfef02cd90f252f0b772f99b13a1e13255ff9eda"
dependencies:
"@dxcli/screen" "^0.0.1"
chalk "^2.3.0"
lodash "^4.17.4"

"@dxcli/screen@^0.0.0":
version "0.0.0"
resolved "https://registry.yarnpkg.com/@dxcli/screen/-/screen-0.0.0.tgz#a85f69bfb00d3961656bad5d7b12f8e603c7ce09"
Expand All @@ -200,13 +227,13 @@
version "0.0.1"
resolved "https://registry.yarnpkg.com/@dxcli/screen/-/screen-0.0.1.tgz#9af4e8d0e5a9475e9e4b5f2da775b0447ff72fc2"

"@dxcli/version@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@dxcli/version/-/version-0.1.4.tgz#6f4da6fb9e8dbb58afcc3cb73974ba32abd573d6"
"@dxcli/version@^0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@dxcli/version/-/version-0.1.5.tgz#4542e8e97fb2efeb19b4694d88c0730d5b97c7dd"
dependencies:
"@dxcli/command" "^0.1.11"
"@dxcli/config" "^0.1.20"
cli-ux "^3.1.3"
"@dxcli/command" "^0.1.13"
"@dxcli/config" "^0.1.22"
cli-ux "^3.1.5"

"@heroku/linewrap@^1.0.0":
version "1.0.0"
Expand Down Expand Up @@ -1716,9 +1743,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.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/fancy-mocha/-/fancy-mocha-0.3.2.tgz#4bcc8dc4f323224ce0733bd1a093d8c485852406"
fancy-test@^0.3.5:
version "0.3.5"
resolved "https://registry.yarnpkg.com/fancy-test/-/fancy-test-0.3.5.tgz#6ad7a38b1da46a62018981e3c80267a01bd023db"
dependencies:
lodash "^4.17.4"
stdout-stderr "^0.1.4"
Expand Down Expand Up @@ -3051,13 +3078,14 @@ mkdirp@0.5.1, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
dependencies:
minimist "0.0.8"

mocha-junit-reporter@^1.16.0:
version "1.16.0"
resolved "https://registry.yarnpkg.com/mocha-junit-reporter/-/mocha-junit-reporter-1.16.0.tgz#3d76af53e2f5e81f9b93406acee1e48b5dd6e6cc"
mocha-junit-reporter@^1.17.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/mocha-junit-reporter/-/mocha-junit-reporter-1.17.0.tgz#2e5149ed40fc5d2e3ca71e42db5ab1fec9c6d85c"
dependencies:
debug "^2.2.0"
md5 "^2.1.0"
mkdirp "~0.5.1"
strip-ansi "^4.0.0"
xml "^1.0.0"

mocha@^5.0.0:
Expand Down

0 comments on commit 39078f0

Please sign in to comment.