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

Commit

Permalink
feat: timeout()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Aug 31, 2018
1 parent 2c8e3cf commit 2652e1c
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 50 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,23 @@ describe('test retries', () => {
})
```

Timeout
-------

Set mocha timeout duration.

```js
const wait = (ms = 10) => new Promise(resolve => setTimeout(resolve, ms))

describe('timeout', () => {
fancy
.timeout(50)
.it('times out after 50ms', async () => {
await wait(100)
})
})
```

Chai
----

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"@types/lodash": "^4.14.116",
"@types/mocha": "^5.2.5",
"@types/nock": "^9.3.0",
"@types/node": "^10.7.1",
"@types/sinon": "^5.0.1",
"@types/node": "^10.9.4",
"@types/sinon": "^5.0.2",
"lodash": "^4.17.10",
"mock-stdin": "^0.3.1",
"stdout-stderr": "^0.1.9"
Expand All @@ -19,14 +19,15 @@
"@oclif/tslint": "^2.0.0",
"chai": "^4.1.2",
"chalk": "^2.4.1",
"http-call": "^5.1.4",
"http-call": "^5.2.2",
"markdown-toc": "^1.2.0",
"mocha": "^5.2.0",
"nock": "^9.6.1",
"sinon": "^6.1.5",
"ts-node": "^7.0.1",
"tslib": "^1.9.3",
"tslint": "^5.11.0",
"typescript": "^3.0.1"
"typescript": "^3.0.3"
},
"engines": {
"node": ">=8.0.0"
Expand All @@ -43,6 +44,7 @@
"repository": "jdxcode/fancy-test",
"scripts": {
"build": "rm -rf lib && tsc",
"version": "markdown-toc -i README.md && git add README.md",
"lint": "tsc -p test --noEmit && tslint -p test -t stylish",
"posttest": "yarn run lint",
"prepublishOnly": "yarn run build",
Expand Down
2 changes: 2 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
if (context.error) throw context.error
}
return context.test(arg1, (cb && cb.length === 2) ? function (done) {
if (context.timeout) this.timeout(context.timeout)
run.call(this, done).catch(done)
} : function () {
if (context.timeout) this.timeout(context.timeout)
return run.call(this)
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import env from './env'
import * as Nock from './nock'
import {stderr, stdin, stdout} from './stdmock'
import stub from './stub'
import timeout from './timeout'

import * as FancyTypes from './types'

Expand All @@ -16,6 +17,7 @@ export const fancy = base
.register('stderr', stderr)
.register('stdout', stdout)
.register('nock', Nock.nock)
.register('timeout', timeout)

export type Fancy = typeof fancy

Expand Down
7 changes: 7 additions & 0 deletions src/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (timeout?: number) => {
return {
init(ctx: {timeout: number}) {
ctx.timeout = timeout!
},
}
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Context {
chain: Plugin<any>[]
error?: Error & {code?: string}
retries?: number
timeout?: number
}

export interface Plugin<I> {
Expand Down
12 changes: 12 additions & 0 deletions test/timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {fancy} from '../src'

// tslint:disable-next-line no-string-based-set-timeout
const wait = (ms = 10) => new Promise(resolve => setTimeout(resolve, ms))

describe('timeout', () => {
fancy
.timeout(10)
.it('does not time out', async () => {
await wait(1)
})
})
Loading

0 comments on commit 2652e1c

Please sign in to comment.