From 295862845b22df096cc1e0bf8131e4d535f688df Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Thu, 25 Jan 2018 12:59:35 -0800 Subject: [PATCH] fix: pass key to add --- README.md | 48 +++++++++++++++++++++-------- src/base.ts | 9 ++++-- src/index.ts | 4 +-- src/{mock.ts => stub.ts} | 0 test/run.test.ts | 4 +-- test/{mock.test.ts => stub.test.ts} | 6 ++-- 6 files changed, 49 insertions(+), 22 deletions(-) rename src/{mock.ts => stub.ts} (100%) rename test/{mock.test.ts => stub.test.ts} (82%) diff --git a/README.md b/README.md index 4040e07..41c9102 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,34 @@ Mocha out of the box often requires a lot of setup and teardown code in `beforeE It might be compatible with other testing libraries as well (e.g. jest), but would require a couple small changes. Let me know if you'd be interested in this. +As an example, here is what a test file might look like for an application setup with fancy-mocha. This chain could partially be stored to a variable for reuse. + +```js +describe('api', () => { + fancy() + // [custom plugin] initializes the db + .initDB({withUser: mockDBUser}) + + // [custom plugin] uses nock to mock out github API + .mockGithubAPI({user: mockGithubUser}) + + // [custom plugin] that calls the API of the app + .call('POST', '/api/user/foo', {id: mockDBUser.id}) + + // add adds to the context object + // fetch the newly created data from the API (can return a promise) + .add('user', ctx => ctx.db.fetchUserAsync(mockDBUser.id)) + + // run just runs arbitary code + // check to ensure the operation was successful + .run(ctx => expect(ctx.user.foo).to.equal('bar')) + + // end is essentially mocha's it(expectation, callback) + // start the test and provide a description + .end('POST /api/user/foo updates the user') +}) +``` + Usage ===== @@ -47,24 +75,22 @@ import {fancy} from 'fancy-mocha' import {expect} from 'chai' ``` -Mock +Stub ---- -(not to be confused with [nock](#nock)) - -Mock any object. Like all fancy plugins, it ensures that it is reset to normal after the test runs. +Stub any object. Like all fancy plugins, it ensures that it is reset to normal after the test runs. ```js import * as os from 'os' -describe('mock tests', () => { +describe('stub tests', () => { fancy() - .mock(os, 'platform', () => 'foobar') + .stub(os, 'platform', () => 'foobar') .end('sets os', () => { expect(os.platform()).to.equal('foobar') }) fancy() - .mock(os, 'platform', sinon.stub().returns('foobar')) + .stub(os, 'platform', sinon.stub().returns('foobar')) .end('uses sinon', () => { expect(os.platform()).to.equal('foobar') expect(os.platform.called).to.equal(true) @@ -118,8 +144,6 @@ But this has a common flaw, if the test does not error, the test will still pass Nock ---- -(not to be confused with [mock](#mock)) - Uses [nock](https://github.com/node-nock/nock) to mock out HTTP calls to external APIs. You'll need to also install nock in your `devDependencies`. Automatically calls `done()` to ensure the calls were made and `cleanAll()` to remove any pending requests. @@ -177,8 +201,8 @@ describe('run', () => { // add to context object fancy() - .add(() => { return {a: 1}}) - .add(() => { return {b: 2}}) + .add('a', () => 1) + .add('b', () => 2) // context will be {a: 1, b: 2} .end('does something with context', context => { // test code @@ -297,7 +321,7 @@ It's easy to create your own plugins to extend fancy. In [dxcli](https://github. 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](https://github.com/jdxcode/fancy-mocha/blob/master/test/base.test.ts) to see the TypeScript types needed. +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 diff --git a/src/base.ts b/src/base.ts index e1dae27..237e56a 100644 --- a/src/base.ts +++ b/src/base.ts @@ -33,7 +33,7 @@ export interface MochaCallback extends Callback = { end(expectation: string, cb?: (context: I) => any): void end(cb?: (context: I) => any): void - add(cb: (context: I) => Promise | O): Fancy + 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} @@ -53,8 +53,11 @@ const fancy = (context: any, plugins: any, await cb(input) }]) }, - add(cb) { - return fancy(context, plugins, [...chain, (input: any) => cb(input)]) + add(key, cb) { + return fancy(context, plugins, [...chain, async (input: any) => { + const output = await cb(input) + return {[key]: output} + }]) }, end(arg1: any, cb: any) { if (_.isFunction(arg1)) { diff --git a/src/index.ts b/src/index.ts index d32ff6e..585e9f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,14 @@ import base, {Base, Fancy, Next, Plugin} from './base' import _catch, {CatchOptions} from './catch' import env, {EnvOptions} from './env' -import mock from './mock' import nock, {NockScope} from './nock' import {stderr, StdmockOptions, stdout} from './stdmock' +import stub from './stub' export const fancy = base .register('catch', _catch) .register('env', env) -.register('mock', mock) +.register('stub', stub) .register('nock', nock) .register('stderr', stderr) .register('stdout', stdout) diff --git a/src/mock.ts b/src/stub.ts similarity index 100% rename from src/mock.ts rename to src/stub.ts diff --git a/test/run.test.ts b/test/run.test.ts index ecb964d..0e73cd0 100644 --- a/test/run.test.ts +++ b/test/run.test.ts @@ -14,8 +14,8 @@ describe('run', () => { describe('add', () => { fancy() - .add(() => Promise.resolve({foo: 'foo'})) - .add(() => Promise.resolve({bar: 'bar'})) + .add('foo', () => 'foo') + .add('bar', () => Promise.resolve('bar')) .run(ctx => expect(ctx).to.include({foo: 'foo', bar: 'bar'})) .end('adds the properties') }) diff --git a/test/mock.test.ts b/test/stub.test.ts similarity index 82% rename from test/mock.test.ts rename to test/stub.test.ts index d2e047f..25f3d86 100644 --- a/test/mock.test.ts +++ b/test/stub.test.ts @@ -7,16 +7,16 @@ import {expect, fancy} from '../src' const os = require('os') const platform = os.platform() -describe('mock', () => { +describe('stub', () => { // from readme fancy() - .mock(os, 'platform', () => 'foobar') + .stub(os, 'platform', () => 'foobar') .end('sets os', () => { expect(os.platform()).to.equal('foobar') }) fancy() - .mock(os, 'platform', sinon.stub().returns('foobar')) + .stub(os, 'platform', sinon.stub().returns('foobar')) .end('uses sinon', () => { expect(os.platform()).to.equal('foobar') expect(os.platform.called).to.equal(true)