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

Commit

Permalink
feat: finally
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 26, 2018
1 parent 74c313e commit 75264f1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extendable utilities for testing
- [Usage](#usage)
* [Stub](#stub)
* [Catch](#catch)
* [Finally](#finally)
* [Nock](#nock)
* [Environment Variables](#environment-variables)
* [Run](#run)
Expand Down Expand Up @@ -143,6 +144,20 @@ it('dont do this', () => {

But this has a common flaw, if the test does not error, the test will still pass. Chai and other assertion libraries have helpers for this, but they still end up with somewhat messy code.

Finally
-------

Run a tasks even if the test errors out.

```js
describe('finally tests', () => {
fancy
.do(() => { throw new Error('x') })
.finally(() => { /* always called */ })
.end('always calls finally')
})
```

Nock
----

Expand Down
6 changes: 6 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
chain: [...context.chain, {run: (input: any) => cb(input)}]
})
},
finally(cb) {
return base({
...context as any,
chain: [...context.chain, {finally: (input: any) => cb(input)}]
})
},
add(key, cb) {
return base({
...context as any,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type Base<I extends Context, T extends Plugins> = {
}
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>
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}}>
} & {[P in keyof T]: (arg1?: T[P]['a1'], arg2?: T[P]['a2'], arg3?: T[P]['a3'], arg4?: T[P]['a4']) => Base<T[P]['output'] & I, T>}

Expand Down
17 changes: 17 additions & 0 deletions test/finally.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// tslint:disable no-console

import {fancy} from '../src'

describe('finally', () => {
// from readme
// fancy
// .do(() => { throw new Error('x') })
// .finally(() => console.log('foo'))
// .end('always calls finally')
// from readme

fancy
// not sure how to actually test this
.finally(() => {})
.end('finally')
})

0 comments on commit 75264f1

Please sign in to comment.