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

feat: timeout() #17

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
- image: node:latest
working_directory: ~/cli
environment:
NYC: "yarn exec nyc -- --nycrc-path node_modules/@anycli/nyc-config/.nycrc"
NYC: "yarn exec nyc -- --nycrc-path node_modules/@oclif/nyc-config/.nycrc"
MOCHA_FILE: "reports/mocha.xml"
steps:
- checkout
Expand All @@ -15,13 +15,12 @@ jobs:
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ checksum "yarn.lock"}}
- v0-yarn-{{checksum ".circleci/config.yml"}}
- run: .circleci/greenkeeper
- run: yarn add -D nyc@11 @anycli/nyc-config@0 mocha-junit-reporter@1 @commitlint/cli@6 @commitlint/config-conventional@6
- run: yarn add -D nyc@13 @oclif/nyc-config@0
- run: |
mkdir -p reports
$NYC yarn test --reporter mocha-junit-reporter
$NYC yarn test
$NYC report --reporter text-lcov > coverage.lcov
curl -s https://codecov.io/bash | bash
- run: yarn exec commitlint -- -x @commitlint/config-conventional --from origin/master
- store_test_results: &store_test_results
path: ~/cli/reports
node-8:
Expand All @@ -34,11 +33,11 @@ jobs:
- add_ssh_keys
- checkout
- restore_cache: *restore_cache
- run: yarn global add @anycli/semantic-release@1 semantic-release@12
- run: yarn global add @oclif/semantic-release@3 semantic-release@15
- run: yarn --frozen-lockfile
- run: |
export PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH
semantic-release -e @anycli/semantic-release
semantic-release -e @oclif/semantic-release
- save_cache:
key: v0-yarn-{{checksum ".circleci/config.yml"}}-{{checksum "yarn.lock"}}
paths:
Expand Down
2 changes: 1 addition & 1 deletion .circleci/greenkeeper
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if [[ ! -z "$GIT_EMAIL" ]] & [[ ! -z "$GIT_USERNAME" ]]; then
fi

if [[ ! -x "$(command -v greenkeeper-lockfile-update)" ]]; then
yarn global add greenkeeper-lockfile@1
yarn global add greenkeeper-lockfile@2
fi

greenkeeper-lockfile-update
Expand Down
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
19 changes: 19 additions & 0 deletions test/timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {expect, 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(100)
.it('sets timeout var', async function () {
expect(this._runnable._timeout).to.equal(100)
await wait(1)
})

fancy
.timeout(100)
.it('sets timeout with done fn', (_, done) => {
wait(1).then(done).catch(done)
})
})
Loading