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

Commit

Permalink
fix: fixed retries
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 27, 2018
1 parent 203124c commit 48ee57c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ extendable utilities for testing
* [Run](#run)
* [Add](#add)
* [Stdout/Stderr Mocking](#stdoutstderr-mocking)
* [Done](#done)
* [Retries](#retries)
* [Chai](#chai)
- [Chaining](#chaining)
- [Custom Plugins](#custom-plugins)
Expand Down Expand Up @@ -287,8 +289,6 @@ 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) => {
Expand All @@ -297,6 +297,25 @@ describe('calls done', () => {
})
```

Retries
-------

Retry the test n times.

```js
let count = 3

describe('test retries', () => {
fancy
.retries(2)
.do(() => {
count--
if (count > 0) throw new Error('x')
})
.it('retries 3 times')
})
```

Chai
----

Expand Down
10 changes: 8 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
}
if (!arg1) arg1 = context.expectation || 'test'
async function run(this: mocha.ITestCallbackContext, done?: Types.MochaDone) {
// reset error if retrying
delete context.error
if (context.retries) this.retries(context.retries)
if (cb) {
context.chain = [...context.chain, {
run: async (input: any) => {
Expand Down Expand Up @@ -94,12 +97,12 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
chain: [...context.chain, {finally: (input: any) => cb(input)}]
})
},
add(key, cb) {
add(key, v) {
return base({
...context as any,
chain: [...context.chain, {
run: async (ctx: any) => {
ctx[key] = await cb(ctx)
ctx[key] = await (_.isFunction(v) ? v(ctx) : v)
}
}]
})
Expand Down Expand Up @@ -135,3 +138,6 @@ export default base(context)
.register('only', () => ({
init: ctx => {ctx.test = it.only}
}))
.register('retries', (count: number) => ({
init: ctx => ctx.retries = count
}))
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Context {
expectation?: string
chain: Plugin<any>[]
error?: Error & {code?: string}
retries?: number
}

export interface Plugin<I> {
Expand Down Expand Up @@ -36,7 +37,7 @@ export interface It<I> {
export type Base<I extends Context, T extends Plugins> = {
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>
add<K extends string, O>(key: K, cb: ((context: I) => Promise<O> | O) | 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>
register<K extends string, O, A1, A2, A3, A4>(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<O & I>): Base<I, T & {[P in K]: {output: O, a1: A1, a2: A2, a3: A3, a4: A4}}>
Expand Down
17 changes: 17 additions & 0 deletions test/retries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// tslint:disable no-console

import {fancy} from '../src'

let count = 3

describe('retries', () => {
// from readme
fancy
.retries(2)
.do(() => {
count--
if (count > 0) throw new Error('x')
})
.it('retries 3 times')
// from readme
})

0 comments on commit 48ee57c

Please sign in to comment.