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

Commit

Permalink
feat: add done
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 26, 2018
1 parent b6fb7ab commit efd646b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----

Expand Down
19 changes: 10 additions & 9 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
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<boolean> => {
context.error = err
Expand All @@ -52,7 +52,8 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
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)
Expand Down
17 changes: 9 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ export interface PluginDef {

export interface Plugins {[k: string]: PluginDef}

export interface It<I> {
(expectation: string, cb?: (context: I, done?: MochaDone) => any): void
(cb?: (context: I, done?: MochaDone) => any): void
}

export type Base<I extends Context, T extends Plugins> = {
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<I>
end: It<I>
add<K extends string, O>(key: K, cb: (context: I) => Promise<O> | O): Base<I & {[P in K]: O}, T>
do<O>(cb: (context: I & O) => any): Base<O & I, T>
finally(cb: (context: I) => any): Base<I, T>
Expand All @@ -45,3 +44,5 @@ export interface EnvOptions {
}

export interface Env extends Plugin<{envs: (typeof process.env)[]}> {}

export type MochaDone = (error?: any) => any
2 changes: 1 addition & 1 deletion test/finally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ describe('finally', () => {
fancy
// not sure how to actually test this
.finally(() => {})
.end('finally')
.it('finally')
})

0 comments on commit efd646b

Please sign in to comment.