Skip to content

Commit

Permalink
feat(only): add it.only(10) syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 10, 2017
1 parent fd24537 commit 80f4c02
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ output:
10 passing (10ms)
```

## Features

* Supports `it.only(n)(...)` syntax to run single text N times

## Related projects

* [focha](https://github.com/bahmutov/focha) - Mocha wrapper
Expand Down
42 changes: 39 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
'use strict'

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

function isFunction (x) {
return typeof x === 'function'
}

function isNumber (x) {
return typeof x === 'number'
}

function isNonNegative (x) {
return isNumber(x) && x >= 0
}

console.assert(isFunction(oldIt), 'missing global it function')
console.assert(isFunction(oldIt.only), 'missing it.only function')

function checkTimes (times) {
console.assert(isNonNegative(times), 'repeat times should be non-negative')
}

function only (times) {
if (!isNumber(times)) {
// probably plain it('name', ...) call
return oldIt.only.apply(null, arguments)
}

checkTimes(times)

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

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

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

return function (title, fn) {
let k
Expand All @@ -19,4 +53,6 @@ function repeatIt (times) {
}
}

repeatIt.only = only

module.exports = repeatIt
4 changes: 4 additions & 0 deletions src/repeat-it-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('repeat-it', () => {
counter += 1
})

it('has .only function', () => {
console.assert(typeof it.only === 'function')
})

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

0 comments on commit 80f4c02

Please sign in to comment.