Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: pass key to add
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 25, 2018
1 parent 308aa59 commit 2958628
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
48 changes: 36 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====

Expand All @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface MochaCallback<U> extends Callback<mocha.ITestCallbackContext, U
export type Fancy<I extends object, T extends Plugins> = {
end(expectation: string, cb?: (context: I) => any): void
end(cb?: (context: I) => any): void
add<O extends object>(cb: (context: I) => Promise<O> | O): Fancy<I & O, T>
add<K extends string, O>(key: K, cb: (context: I) => Promise<O> | O): Fancy<I & {[P in K]: O}, T>
run(cb: (context: I) => any): Fancy<I, T>
} & {[P in keyof T]: (arg1?: T[P][2], arg2?: T[P][3], arg3?: T[P][4], arg4?: T[P][5]) => Fancy<I & T[P][0], T>}

Expand All @@ -53,8 +53,11 @@ const fancy = <I extends object, T extends Plugins>(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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
6 changes: 3 additions & 3 deletions test/mock.test.ts → test/stub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2958628

Please sign in to comment.