From af720c95783493d4cec80ff62e07fd0249d95488 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Thu, 25 Jan 2018 23:45:21 -0800 Subject: [PATCH] fix: do not require fancy() --- README.md | 108 +++++++++++------------ src/base.ts | 204 +++++++++++++++++++++++-------------------- src/catch.ts | 18 ++-- src/env.ts | 23 +++-- src/index.ts | 11 +-- src/nock.ts | 27 +++--- src/stdmock.ts | 30 ++----- src/stub.ts | 23 +++-- test/base.test.ts | 22 ++--- test/catch.test.ts | 26 +++--- test/env.test.ts | 6 +- test/nock.test.ts | 2 +- test/run.test.ts | 10 +-- test/stdmock.test.ts | 20 ++--- test/stub.test.ts | 6 +- 15 files changed, 261 insertions(+), 275 deletions(-) diff --git a/README.md b/README.md index ef33165..1b0c111 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ As an example, here is what a test file might look like for an application setup ```js describe('api', () => { - fancy() + fancy // [custom plugin] initializes the db .initDB({withUser: mockDBUser}) @@ -61,7 +61,7 @@ describe('api', () => { // end is essentially mocha's it(expectation, callback) // start the test and provide a description - .end('POST /api/user/foo updates the user') + .it('POST /api/user/foo updates the user') }) ``` @@ -85,15 +85,15 @@ Stub any object. Like all fancy plugins, it ensures that it is reset to normal a import * as os from 'os' describe('stub tests', () => { - fancy() + fancy .stub(os, 'platform', () => 'foobar') - .end('sets os', () => { + .it('sets os', () => { expect(os.platform()).to.equal('foobar') }) - fancy() + fancy .stub(os, 'platform', sinon.stub().returns('foobar')) - .end('uses sinon', () => { + .it('uses sinon', () => { expect(os.platform()).to.equal('foobar') expect(os.platform.called).to.equal(true) }) @@ -107,25 +107,25 @@ catch errors in a declarative way. By default, ensures they are actually thrown ```js describe('catch tests', () => { - fancy() + fancy .run(() => { throw new Error('foobar') }) .catch(/foo/) - .end('uses regex') + .it('uses regex') - fancy() + fancy .run(() => { throw new Error('foobar') }) .catch('foobar') - .end('uses string') + .it('uses string') - fancy() + fancy .run(() => { throw new Error('foobar') }) .catch(err => expect(err.message).to.match(/foo/)) - .end('uses function') + .it('uses function') - fancy() + fancy // this would normally raise because there is no error being thrown .catch('foobar', {raiseIfNotThrown: false}) - .end('do not error if not thrown') + .it('do not error if not thrown') }) ``` @@ -151,13 +151,13 @@ Automatically calls `done()` to ensure the calls were made and `cleanAll()` to r ```js describe('nock tests', () => { - fancy() + fancy .nock('https://api.github.com', nock => { nock .get('/me') .reply(200, {name: 'jdxcode'}) }) - .end('mocks http call to github', async () => { + .it('mocks http call to github', async () => { const {body: user} = await HTTP.get('https://api.github.com/me') expect(user).to.have.property('name', 'jdxcode') }) @@ -171,16 +171,16 @@ Sometimes it's helpful to clear out environment variables before running tests o ```js describe('env tests', () => { - fancy() + fancy .env({FOO: 'BAR'}) - .end('mocks FOO', () => { + .it('mocks FOO', () => { expect(process.env.FOO).to.equal('BAR') expect(process.env).to.not.deep.equal({FOO: 'BAR'}) }) - fancy() + fancy .env({FOO: 'BAR'}, {clear: true}) - .end('clears all env vars', () => { + .it('clears all env vars', () => { expect(process.env).to.deep.equal({FOO: 'BAR'}) }) }) @@ -193,20 +193,20 @@ Run some arbitrary code within the pipeline. Useful to create custom logic and d ```js describe('run', () => { - fancy() + fancy .stdout() .run(() => console.log('foo')) .run(({stdout}) => expect(stdout).to.equal('foo\n')) - .end('runs this callback last', () => { + .it('runs this callback last', () => { // test code }) // add to context object - fancy() + fancy .add('a', () => 1) .add('b', () => 2) // context will be {a: 1, b: 2} - .end('does something with context', context => { + .it('does something with context', context => { // test code }) }) @@ -220,11 +220,11 @@ Can return a promise or not. ```js describe('add', () => { - fancy() + fancy .add('foo', () => 'foo') .add('bar', () => Promise.resolve('bar')) .run(ctx => expect(ctx).to.include({foo: 'foo', bar: 'bar'})) - .end('adds the properties') + .it('adds the properties') }) ``` @@ -240,24 +240,24 @@ You can use the library [stdout-stderr](https://npm.im/stdout-stderr) directly f import chalk from 'chalk' describe('stdmock tests', () => { - fancy() + fancy .stdout() - .end('mocks stdout', output => { + .it('mocks stdout', output => { console.log('foobar') expect(output.stdout).to.equal('foobar\n') }) - fancy() + fancy .stderr() - .end('mocks stderr', output => { + .it('mocks stderr', output => { console.error('foobar') expect(output.stderr).to.equal('foobar\n') }) - fancy() + fancy .stdout() .stderr() - .end('mocks stdout and stderr', output => { + .it('mocks stdout and stderr', output => { console.log('foo') console.error('bar') expect(output.stdout).to.equal('foo\n') @@ -275,9 +275,9 @@ This library includes [chai](https://npm.im/chai) preloaded with [chai-as-promis import {expect, fancy} from 'fancy-mocha' describe('has chai', () => { - fancy() + fancy .env({FOO: 'BAR'}) - .end('expects FOO=bar', () => { + .it('expects FOO=bar', () => { expect(process.env.FOO).to.equal('BAR') }) }) @@ -292,19 +292,19 @@ For example: ```js describe('my suite', () => { - let setupDB = fancy() + let setupDB = fancy .run(() => setupDB()) .env({FOO: 'FOO'}) setupDB .stdout() - .end('tests with stdout mocked', () => { + .it('tests with stdout mocked', () => { // test code }) setupDB .env({BAR: 'BAR'}) - .end('also mocks the BAR environment variable', () => { + .it('also mocks the BAR environment variable', () => { // test code }) }) @@ -314,7 +314,7 @@ Using [run](#run) you can really maximize this ability. In fact, you don't even ```js describe('my suite', () => { - let setupDB = fancy() + let setupDB = fancy .run(() => setupDB()) .catch(/spurious db error/) .run(() => setupDeps()) @@ -325,10 +325,10 @@ describe('my suite', () => { } testMyApp({info: 'test run a'}) - .end('tests a') + .it('tests a') testMyApp({info: 'test run b'}) - .end('tests b') + .it('tests b') }) ``` @@ -337,38 +337,36 @@ Custom Plugins It's easy to create your own plugins to extend fancy. In [dxcli](https://github.com/dxcli/dxcli) we use fancy to create [custom command testers](https://github.com/dxcli/example-multi-cli-typescript/blob/master/test/commands/hello.test.ts). -A plugin is a function that receives a `next` callback that it must call to execute the next plugin in the chain. - Here is an example that creates a counter that could be used to label each test run. See the [actual test](test/base.test.ts) to see the TypeScript types needed. ```js let count = 0 -const counter = prefix => () => { - count++ - return {count, testLabel: `${prefix}${count}`} -} - -// note that .register() MUST be called on a non-instantiated fancy object. -const myFancy = fancy -.register('count', counter) +fancy = fancy +.register('count', prefix => { + return { + run(ctx) { + ctx.count = ++count + ctx.testLabel = `${prefix}${count}` + } + } +}) describe('register', () => { - myFancy() + fancy .count('test-') - .end('is test #1', context => { + .it('is test #1', context => { expect(context.count).to.equal(1) expect(context.testLabel).to.equal('test-1') }) - myFancy() + fancy .count('test-') - .end('is test #2', context => { + .it('is test #2', context => { expect(context.count).to.equal(2) expect(context.testLabel).to.equal('test-2') }) }) -) ``` TypeScript diff --git a/src/base.ts b/src/base.ts index 237e56a..4804018 100644 --- a/src/base.ts +++ b/src/base.ts @@ -2,113 +2,131 @@ // tslint:disable no-unused import * as _ from 'lodash' -import * as mocha from 'mocha' -export interface Next { - (output: O): Promise -} +export type PluginBuilder = (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin -export interface PluginBuilder { - (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4): Plugin -} -export interface Plugin { - (context: any): Promise | O | void - init?(context: any): InitO - finally?(context: any): any - catch?(context: any): any +export interface Plugin { + run?(context: I): any + init?(context: I): any + finally?(context: I): any + catch?(context: I): any } -export interface Plugins {[k: string]: [object]} +export interface Context { + test: (typeof it | typeof it.skip) + plugins: {[k: string]: PluginBuilder} + expectation?: string + chain: Plugin[] + error?: Error & {code?: string} +} -export interface Base { - (): Fancy<{ test: typeof it, error?: Error }, T> - register(k: K, v: PluginBuilder): Base +const context: Context = { + test: it, + plugins: {}, + chain: [], } -export interface Callback { - (this: T, context: U): any +export interface Plugins {[k: string]: [object, any, any, any, any]} + +export interface It { + (expectation: string, cb?: (context: I) => any): void + (cb?: (context: I) => any): void } -export interface MochaCallback extends Callback {} -export type Fancy = { - end(expectation: string, cb?: (context: I) => any): void - end(cb?: (context: I) => any): void +export type Fancy = { + it: It + end: It add(key: K, cb: (context: I) => Promise | O): Fancy - run(cb: (context: I) => any): Fancy -} & {[P in keyof T]: (arg1?: T[P][2], arg2?: T[P][3], arg3?: T[P][4], arg4?: T[P][5]) => Fancy} + do(cb: (context: I) => any): Fancy + register(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin): Fancy +} & {[P in keyof T]: (arg1?: T[P][1], arg2?: T[P][2], arg3?: T[P][3], arg4?: T[P][4]) => Fancy} -const fancy = (context: any, plugins: any, chain: Plugin[] = []): Fancy => { +const base = (context: I): Fancy => { + const end = (arg1: any, cb: any) => { + context = assignWithProps({}, context) + if (_.isFunction(arg1)) { + cb = arg1 + arg1 = undefined + } + if (!arg1) arg1 = context.expectation || 'test' + if (cb) { + context.chain = [...context.chain, { + run: async (input: any) => { + await cb(input) + } + }] + } + return context.test(arg1, async function () { + for (let i = 0; i < context.chain.length; i++) { + const handleError = async (err: Error): Promise => { + context.error = err + i++ + const handler = context.chain[i] + if (!handler || !handler.catch) return false + try { + await handler.catch(context) + delete context.error + return true + } catch (err) { + return handleError(err) + } + } + const next = context.chain[i] + try { + if (next.run) await next.run(context) + } catch (err) { + if (!await handleError(err)) break + } + } + for (let p of context.chain.reverse()) { + if (p.finally) await p.finally(context) + } + if (context.error) throw context.error + }) + } return { - ...Object.entries(plugins) - .reduce((fns, [k, v]) => { - fns[k] = (...args: any[]) => { + ...Object.entries(context.plugins) + .reduce((plugins, [k, v]) => { + plugins[k] = (...args: any[]) => { const plugin = v(...args) - if (plugin.init) context = assignWithProps({}, context, v.init(context)) - return fancy(context, plugins, [...chain, plugin]) + if (plugin.init) plugin.init(context) + return base({ + ...context as any, + chain: [...context.chain, plugin], + }) } - return fns + return plugins }, {} as any), - run(cb) { - return fancy(context, plugins, [...chain, async (input: any) => { - await cb(input) - }]) + register(k: any, v: any) { + return base({ + ...context as any, + plugins: { + ...context.plugins, + [k]: v, + }, + }) }, - add(key, cb) { - return fancy(context, plugins, [...chain, async (input: any) => { - const output = await cb(input) - return {[key]: output} - }]) + do(cb) { + return base({ + ...context as any, + chain: [...context.chain, {run: (input: any) => cb(input)}] + }) }, - end(arg1: any, cb: any) { - if (_.isFunction(arg1)) { - cb = arg1 - arg1 = undefined - } - if (!arg1) arg1 = context.expectation || 'test' - if (cb) { - chain = [...chain, async (input: any) => { - await cb(input) - }] - } - return context.test(arg1, async function () { - for (let i = 0; i < chain.length; i++) { - const handleError = async (err: Error): Promise => { - context.error = err - i++ - const handler = chain[i] - if (!handler || !handler.catch) return false - try { - await handler.catch(context) - delete context.error - return true - } catch (err) { - return handleError(err) - } - } - const next = chain[i] - try { - context = assignWithProps({}, context, await next(context)) - } catch (err) { - if (!await handleError(err)) break + add(key, cb) { + return base({ + ...context as any, + chain: [...context.chain, { + run: async (ctx: any) => { + ctx[key] = await cb(ctx) } - } - for (let p of chain.reverse()) { - if (p.finally) await p.finally(context) - } - if (context.error) throw context.error + }] }) }, + end, + it: end, } } -function base(plugins: any): Base { - const f = (() => fancy({ - test: it, - }, plugins)) as any - f.register = (k: string, v: any) => base({...plugins as any, [k]: v}) - return f -} - function assignWithProps(target: any, ...sources: any[]) { sources.forEach(source => { if (!source) return @@ -128,14 +146,10 @@ function assignWithProps(target: any, ...sources: any[]) { return target } -export default base<{}>({}) -.register('skip', () => { - const plugin = (() => {}) as any - plugin.init = () => ({test: it.skip}) - return plugin -}) -.register('only', () => { - const plugin = (() => {}) as any - plugin.init = () => ({test: it.only}) - return plugin -}) +export default base(context) +.register('skip', () => ({ + init: ctx => {ctx.test = it.skip} +})) +.register('only', () => ({ + init: ctx => {ctx.test = it.only} +})) diff --git a/src/catch.ts b/src/catch.ts index c3fb0d3..3659c0e 100644 --- a/src/catch.ts +++ b/src/catch.ts @@ -1,20 +1,15 @@ import * as _ from 'lodash' -// import {Plugin} from '.' import {expect} from './chai' -export interface CatchOptions { - raiseIfNotThrown?: boolean -} - -export default ((arg: RegExp | string | ((err: Error) => any), opts: CatchOptions = {}) => { - const plugin = (() => { +export default (arg: RegExp | string | ((err: Error) => any), opts: {raiseIfNotThrown?: boolean} = {}) => ({ + run() { if (opts.raiseIfNotThrown !== false) { throw new Error('expected error to be thrown') } - }) as any - plugin.catch = (context: any) => { - const err = context.error + }, + catch(ctx: {error: Error}) { + const err = ctx.error if (_.isRegExp(arg)) { expect(err.message).to.match(arg) } else if (_.isString(arg)) { @@ -24,6 +19,5 @@ export default ((arg: RegExp | string | ((err: Error) => any), opts: CatchOption } else { throw new Error('no arg provided to catch') } - } - return plugin + }, }) diff --git a/src/env.ts b/src/env.ts index 173ae20..e5a3600 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,17 +1,16 @@ -import {Plugin} from '.' - export interface EnvOptions { clear?: boolean } -export default (env: {[k: string]: string | undefined}, opts: EnvOptions = {}) => { - const original = process.env - const plugin = (() => { +export default (env: {[k: string]: string | undefined}, opts: EnvOptions = {}) => ({ + run(ctx: {envs: (typeof process.env)[]}) { + ctx.envs = ctx.envs || [] + ctx.envs.push(process.env) if (opts.clear) process.env = {...env} - else process.env = {...original, ...env} - }) as Plugin - plugin.finally = () => { - process.env = original - } - return plugin -} + else process.env = {...process.env, ...env} + }, + finally(ctx: {envs: (typeof process.env)[]}) { + const env = ctx.envs.pop() + if (env) process.env = env + }, +}) diff --git a/src/index.ts b/src/index.ts index 585e9f7..e5ce13e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ -import base, {Base, Fancy, Next, Plugin} from './base' -import _catch, {CatchOptions} from './catch' +import base, {Context, Fancy, Plugin} from './base' +import _catch from './catch' import env, {EnvOptions} from './env' import nock, {NockScope} from './nock' -import {stderr, StdmockOptions, stdout} from './stdmock' +import {stderr, stdout} from './stdmock' import stub from './stub' export const fancy = base @@ -14,14 +14,11 @@ export const fancy = base .register('stdout', stdout) export { - Base, + Context, Fancy, Plugin, - Next, - CatchOptions, EnvOptions, NockScope, - StdmockOptions, } export default fancy export * from './chai' diff --git a/src/nock.ts b/src/nock.ts index a9ea29d..88c8df4 100644 --- a/src/nock.ts +++ b/src/nock.ts @@ -1,23 +1,20 @@ import * as Nock from 'nock' -import {Plugin} from './base' - -export type NockCallback = (nock: Nock.Scope) => any - -export default (host: string, cb: NockCallback) => { +export default (host: string, cb: (nock: Nock.Scope) => any) => { const nock: typeof Nock = require('nock') const intercepter = nock(host) - const plugin = (async ctx => { - await cb(intercepter) - const count = (ctx.nock as number || 0) + 1 - return {nock: count} - }) as Plugin<{nock: number}> - plugin.finally = ctx => { - if (!ctx.error) intercepter.done() - ctx.nock-- - if (ctx.nock === 0) nock.cleanAll() + return { + async run(ctx: {nock: number}) { + ctx.nock = ctx.nock || 0 + await cb(intercepter) + ctx.nock++ + }, + finally(ctx: {error?: Error, nock: number}) { + if (!ctx.error) intercepter.done() + ctx.nock-- + if (ctx.nock === 0) nock.cleanAll() + }, } - return plugin } export {Scope as NockScope} from 'nock' diff --git a/src/stdmock.ts b/src/stdmock.ts index a3bfa46..d41e4f5 100644 --- a/src/stdmock.ts +++ b/src/stdmock.ts @@ -1,30 +1,18 @@ import * as mock from 'stdout-stderr' -import {Plugin} from './base' - -export type Return = { - readonly [P in T]: string -} - -export interface StdmockOptions { - print?: boolean - stripColor?: boolean -} - -const create = (std: T) => (opts: StdmockOptions = {}) => { - const plugin = (async () => { +const create = (std: T) => (opts: {print?: boolean, stripColor?: boolean} = {}) => ({ + run(ctx: {readonly [P in T]: string}) { mock[std].start() mock[std].print = opts.print === true mock[std].stripColor = opts.stripColor !== false - return { - get [std]() { return mock[std].output } - } as Return - }) as Plugin> - plugin.finally = () => { + Object.defineProperty(ctx, std, { + get: () => mock[std].output + }) + }, + finally() { mock[std].stop() - } - return plugin -} + }, +}) export const stdout = create('stdout') export const stderr = create('stderr') diff --git a/src/stub.ts b/src/stub.ts index 90aa6c0..8e82da0 100644 --- a/src/stub.ts +++ b/src/stub.ts @@ -1,19 +1,16 @@ import * as _ from 'lodash' -import {Plugin} from './base' - /** * mocks an object's property */ -export default (object: any, path: string, value: any) => { - let original = _.get(object, path) - const plugin = (() => { +export default (object: any, path: string, value: any) => ({ + run(ctx: {stubs: any[]}) { + ctx.stubs = ctx.stubs || [] + ctx.stubs.push(_.get(object, path)) _.set(object, path, value) - }) as Plugin - - plugin.finally = () => { - _.set(object, path, original) - } - - return plugin -} + }, + finally(ctx: {stubs: any[]}) { + const stub = ctx.stubs.pop() + _.set(object, path, stub) + }, +}) diff --git a/test/base.test.ts b/test/base.test.ts index 3471323..667559f 100644 --- a/test/base.test.ts +++ b/test/base.test.ts @@ -2,26 +2,28 @@ import {expect, fancy} from '../src' let count = 0 -const counter = (prefix: string) => () => { - count++ - return {count, testLabel: `${prefix}${count}`} -} - // note that .register() MUST be called on a non-instantiated fancy object. const myFancy = fancy -.register('count', counter) +.register('count', (prefix: string) => { + return { + run(ctx: {count: number, testLabel: string}) { + ctx.count = ++count + ctx.testLabel = `${prefix}${count}` + } + } +}) describe('register', () => { - myFancy() + myFancy .count('test-') - .end('is test #1', context => { + .it('is test #1', context => { expect(context.count).to.equal(1) expect(context.testLabel).to.equal('test-1') }) - myFancy() + myFancy .count('test-') - .end('is test #2', context => { + .it('is test #2', context => { expect(context.count).to.equal(2) expect(context.testLabel).to.equal('test-2') }) diff --git a/test/catch.test.ts b/test/catch.test.ts index f4d558e..38a56f3 100644 --- a/test/catch.test.ts +++ b/test/catch.test.ts @@ -4,43 +4,43 @@ import {expect, fancy} from '../src' describe('catch', () => { // from readme - fancy() - .run(() => { throw new Error('foobar') }) + fancy + .do(() => { throw new Error('foobar') }) .catch(/foo/) .end('uses regex') - fancy() - .run(() => { throw new Error('foobar') }) + fancy + .do(() => { throw new Error('foobar') }) .catch('foobar') .end('uses string') - fancy() - .run(() => { throw new Error('foobar') }) + fancy + .do(() => { throw new Error('foobar') }) .catch(err => expect(err.message).to.match(/foo/)) .end('uses function') - fancy() + fancy // this would normally raise because there is no error being thrown .catch('foobar', {raiseIfNotThrown: false}) .end('do not error if not thrown') // from readme - fancy() - .run(() => { throw new Error('foobar') }) + fancy + .do(() => { throw new Error('foobar') }) .catch() .catch('no arg provided to catch') .end('errors if no args passed') - fancy() + fancy .catch() .catch('expected error to be thrown') .end('errors if no error thrown') - fancy() + fancy .stdout() - .run(() => { throw new Error('x') }) + .do(() => { throw new Error('x') }) .catch('x') - .run(() => console.log('foobar')) + .do(() => console.log('foobar')) .end('continues running', ctx => { expect(ctx.stdout).to.equal('foobar\n') }) diff --git a/test/env.test.ts b/test/env.test.ts index df55741..d2dcebd 100644 --- a/test/env.test.ts +++ b/test/env.test.ts @@ -4,20 +4,20 @@ import {expect, fancy} from '../src' describe('env', () => { // from readme - fancy() + fancy .env({FOO: 'BAR'}) .end('mocks FOO', () => { expect(process.env.FOO).to.equal('BAR') expect(process.env).to.not.deep.equal({FOO: 'BAR'}) }) - fancy() + fancy .env({FOO: 'BAR'}, {clear: true}) .end('clears all env vars', () => { expect(process.env).to.deep.equal({FOO: 'BAR'}) }) - fancy() + fancy .env({FOO: 'BAR'}) .stdout() .end('works with stdout', output => { diff --git a/test/nock.test.ts b/test/nock.test.ts index b8a160b..62d7624 100644 --- a/test/nock.test.ts +++ b/test/nock.test.ts @@ -4,7 +4,7 @@ import {expect, fancy} from '../src' describe('nock', () => { // from readme - fancy() + fancy .nock('https://api.github.com', nock => { nock .get('/me') diff --git a/test/run.test.ts b/test/run.test.ts index 0e73cd0..8f7f34e 100644 --- a/test/run.test.ts +++ b/test/run.test.ts @@ -3,19 +3,19 @@ import {expect, fancy} from '../src' describe('run', () => { - fancy() + fancy .stdout() - .run(() => console.log('foo')) - .run(({stdout}) => expect(stdout).to.equal('foo\n')) + .do(() => console.log('foo')) + .do(({stdout}) => expect(stdout).to.equal('foo\n')) .end('runs this callback last', () => { // test code }) }) describe('add', () => { - fancy() + fancy .add('foo', () => 'foo') .add('bar', () => Promise.resolve('bar')) - .run(ctx => expect(ctx).to.include({foo: 'foo', bar: 'bar'})) + .do(ctx => expect(ctx).to.include({foo: 'foo', bar: 'bar'})) .end('adds the properties') }) diff --git a/test/stdmock.test.ts b/test/stdmock.test.ts index 9d03c2e..533e76b 100644 --- a/test/stdmock.test.ts +++ b/test/stdmock.test.ts @@ -5,7 +5,7 @@ import chalk from 'chalk' import {expect, fancy} from '../src' describe('stdout', () => { - fancy() + fancy .stderr() .stdout() .end('logs', output => { @@ -14,7 +14,7 @@ describe('stdout', () => { expect(output.stdout).to.equal('foo\n') }) - fancy() + fancy .stdout() .end('logs twice', output => { console.log('foo') @@ -23,14 +23,14 @@ describe('stdout', () => { expect(output.stdout).to.equal('foo\nbar\n') }) - fancy() + fancy .stderr() .end('writes to stderr', output => { console.error('foo') expect(output.stderr).to.equal('foo\n') }) - fancy() + fancy .stdout() .stderr() .end('writes to both', output => { @@ -40,7 +40,7 @@ describe('stdout', () => { expect(output.stdout).to.equal('bar\n') }) - fancy() + fancy .stdout() .end('strips colors by default', output => { console.log(chalk.red('foobar')) @@ -48,28 +48,28 @@ describe('stdout', () => { }) // from readme - fancy() + fancy .stdout() .end('mocks stdout', output => { console.log('foobar') expect(output.stdout).to.equal('foobar\n') }) - fancy() + fancy .stdout({print: true}) .end('mocks stdout but also prints to screen', output => { console.log('foobar') expect(output.stdout).to.equal('foobar\n') }) - fancy() + fancy .stderr() .end('mocks stderr', output => { console.error('foobar') expect(output.stderr).to.equal('foobar\n') }) - fancy() + fancy .stdout() .stderr() .end('mocks stdout and stderr', output => { @@ -79,7 +79,7 @@ describe('stdout', () => { expect(output.stderr).to.equal('bar\n') }) - fancy() + fancy .stdout({stripColor: false}) .end('mocks stdout but does not strip the color codes', output => { console.log(chalk.red('foobar')) diff --git a/test/stub.test.ts b/test/stub.test.ts index 25f3d86..eebd089 100644 --- a/test/stub.test.ts +++ b/test/stub.test.ts @@ -9,13 +9,13 @@ const platform = os.platform() describe('stub', () => { // from readme - fancy() + fancy .stub(os, 'platform', () => 'foobar') .end('sets os', () => { expect(os.platform()).to.equal('foobar') }) - fancy() + fancy .stub(os, 'platform', sinon.stub().returns('foobar')) .end('uses sinon', () => { expect(os.platform()).to.equal('foobar') @@ -23,7 +23,7 @@ describe('stub', () => { }) // from readme - fancy() + fancy .stdout() .end('resets os', output => { console.log(os.platform())