diff --git a/README.md b/README.md index 4cbc3bc..2120edf 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,22 @@ describe('stdmock tests', () => { }) ``` +Done +---- + +You can get the mocha `done()` callback by passing in a second argument. + +```js +import {expect, fancy} from 'fancy-test' + +describe('calls done', () => { + fancy + .it('expects FOO=bar', (_, done) => { + done() + }) +}) +``` + Chai ---- diff --git a/src/base.ts b/src/base.ts index a8c792a..1d7b5e0 100644 --- a/src/base.ts +++ b/src/base.ts @@ -19,14 +19,14 @@ const base = (context: I): Types.Base => { 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 () { + async function run(done?: Types.MochaDone) { + if (cb) { + context.chain = [...context.chain, { + run: async (input: any) => { + await cb(input, done) + } + }] + } for (let i = 0; i < context.chain.length; i++) { const handleError = async (err: Error): Promise => { context.error = err @@ -52,7 +52,8 @@ const base = (context: I): Types.Base => { if (p.finally) await p.finally(context) } if (context.error) throw context.error - }) + } + return context.test(arg1, (cb && cb.length === 2) ? done => run(done) : () => run()) } return { ...Object.entries(context.plugins) diff --git a/src/types.ts b/src/types.ts index 989c58c..653130a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -25,15 +25,14 @@ export interface PluginDef { export interface Plugins {[k: string]: PluginDef} +export interface It { + (expectation: string, cb?: (context: I, done?: MochaDone) => any): void + (cb?: (context: I, done?: MochaDone) => any): void +} + export type Base = { - it: { - (expectation: string, cb?: (context: I) => any): void - (cb?: (context: I) => any): void - } - end: { - (expectation: string, cb?: (context: I) => any): void - (cb?: (context: I) => any): void - } + it: It + end: It add(key: K, cb: (context: I) => Promise | O): Base do(cb: (context: I & O) => any): Base finally(cb: (context: I) => any): Base @@ -45,3 +44,5 @@ export interface EnvOptions { } export interface Env extends Plugin<{envs: (typeof process.env)[]}> {} + +export type MochaDone = (error?: any) => any diff --git a/test/finally.test.ts b/test/finally.test.ts index 19911aa..b610488 100644 --- a/test/finally.test.ts +++ b/test/finally.test.ts @@ -13,5 +13,5 @@ describe('finally', () => { fancy // not sure how to actually test this .finally(() => {}) - .end('finally') + .it('finally') })