Skip to content

Commit

Permalink
feat(works): repeat a test as many times as necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 9, 2017
1 parent ecff924 commit 7adf0dc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- '7'
- '6'
- '4'
before_script:
- npm prune
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,59 @@
[![semantic-release][semantic-image] ][semantic-url]
[![js-standard-style][standard-image]][standard-url]

## Why

You know a particularly flaky test can drive you crazy? This module makes it
simple to run any test as many times as you want to figure out the flaky
behavior.

## Install

Requires [Node](https://nodejs.org/en/) version 6 or above.
Requires [Node](https://nodejs.org/en/).

```sh
npm install --save repeat-it
npm install --save-dev repeat-it
```

## Use

A Mocha / BDD test you want to repeat

### before

```js
describe('repeat-it', () => {
it('repeated test', () => {})
})
```

### after

```js
const it = require('repeat-it')
describe('repeat-it', () => {
it(10)('repeated test', () => {})
})
```

output:

```
repeat-it
✓ 1 repeated test
✓ 2 repeated test
✓ 3 repeated test
✓ 4 repeated test
✓ 5 repeated test
✓ 6 repeated test
✓ 7 repeated test
✓ 8 repeated test
✓ 9 repeated test
✓ 10 repeated test
10 passing (10ms)
```

### Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "repeat-it",
"description": "Run a Mocha BDD \"it\" test as many times as you want",
"version": "1.0.0",
"version": "0.0.0-development",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": "https://github.com/bahmutov/repeat-it/issues",
"config": {
Expand Down Expand Up @@ -60,21 +60,31 @@
"secure": "nsp check",
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"test": "npm run unit",
"unit": "mocha src/*-spec.js"
"unit": "mocha src/*-spec.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"release": {
"analyzeCommits": "simple-commit-message"
"analyzeCommits": "simple-commit-message",
"generateNotes": "github-post-release",
"verifyRelease": {
"path": "dont-crack",
"test-against": []
}
},
"devDependencies": {
"ban-sensitive-files": "1.9.0",
"dependency-check": "2.9.1",
"deps-ok": "1.2.0",
"dont-crack": "1.2.1",
"git-issues": "1.3.1",
"github-post-release": "1.13.0",
"license-checker": "13.0.3",
"mocha": "3.5.0",
"nsp": "2.7.0",
"pre-git": "3.15.3",
"prettier-standard": "6.0.0",
"semantic-release": "6.3.6",
"simple-commit-message": "3.3.1",
"standard": "10.0.3"
}
}
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
'use strict'

const oldIt = global.it
console.assert(typeof oldIt === 'function', 'missing global it function')

function repeatIt (times) {
if (typeof times !== 'number') {
// probably plain it('name', ...) call
return oldIt.apply(null, arguments)
}

module.exports = true
console.assert(times >= 0, 'repeat times should be non-negative')

return function (title, fn) {
let k
for (k = 0; k < times; k += 1) {
oldIt(k + 1 + ' ' + title, fn)
}
}
}

module.exports = repeatIt
13 changes: 9 additions & 4 deletions src/repeat-it-spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'


/* eslint-env mocha */
const repeatIt = require('.')
const it = require('.')

describe('repeat-it', () => {
it('write this test', () => {
console.assert(repeatIt, 'should export something')
let counter = 0

it(10)('repeated test', () => {
counter += 1
})

after(() => {
console.assert(counter === 10, 'runs ' + counter + ' times')
})
})

0 comments on commit 7adf0dc

Please sign in to comment.