jest-theories
Advanced tools
Comparing version
@@ -15,3 +15,3 @@ 'use strict'; | ||
var theoretically = function theoretically(testName, theories, testFunc) { | ||
var theoretically = function theoretically(testNameCreator, theories, testFunc) { | ||
if (!test) { | ||
@@ -24,4 +24,12 @@ throw new Error('Jest test global must be accessible to use jest-theories'); | ||
var isFunctionTestNameCreator = typeof testNameCreator === 'function'; | ||
if (!isFunctionTestNameCreator && typeof testNameCreator !== 'string') { | ||
throw new Error('Test name creator must be a string or a function'); | ||
} | ||
theories.forEach(function (theory, idx) { | ||
test((0, _stringFormat2.default)(testName, Object.assign({}, theory, { "$idx": idx, "$no": idx + 1 })), testFunc.bind(undefined, theory)); | ||
var testName = isFunctionTestNameCreator ? testNameCreator(theory, idx) : (0, _stringFormat2.default)(testNameCreator, Object.assign({}, theory, { "$idx": idx, "$no": idx + 1 })); | ||
test(testName, testFunc.bind(undefined, theory)); | ||
}); | ||
@@ -28,0 +36,0 @@ }; |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
declare function theoretically<T>(description: string, theories: T[], testFunction: (theory: T) => void): void; | ||
declare function theoretically<T>(description: string | ((theory: T, index: number) => string), theories: T[], testFunction: (theory: T) => void): void; | ||
export default theoretically; |
14
index.js
import format from 'string-format'; | ||
const theoretically = (testName, theories, testFunc) => { | ||
const theoretically = (testNameCreator, theories, testFunc) => { | ||
if (!test) { | ||
@@ -11,5 +11,15 @@ throw new Error('Jest test global must be accessible to use jest-theories'); | ||
const isFunctionTestNameCreator = typeof testNameCreator === 'function'; | ||
if(!isFunctionTestNameCreator && typeof testNameCreator !== 'string') { | ||
throw new Error('Test name creator must be a string or a function') | ||
} | ||
theories.forEach((theory, idx) => { | ||
const testName = isFunctionTestNameCreator | ||
? testNameCreator(theory, idx) | ||
: format(testNameCreator, Object.assign({}, theory, { "$idx": idx, "$no": idx + 1 })); | ||
test( | ||
format(testName, Object.assign({}, theory, { "$idx": idx, "$no": idx + 1 })), | ||
testName, | ||
testFunc.bind(this, theory) | ||
@@ -16,0 +26,0 @@ ); |
{ | ||
"name": "jest-theories", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "build": "babel index.js --out-dir dist" |
@@ -31,5 +31,25 @@ # jest-theories | ||
If you want to do more complex test name creation you can supply a function which takes the parameters of the theory and the index | ||
``` | ||
import theoretically from 'jest-theories'; | ||
describe('Bigger than 1000', () => { | ||
const theories = [ | ||
{input: 100, expected: false}, | ||
{input: 1000, expected: false}, | ||
{input: 10000, expected: true}, | ||
{input: 100000, expected: true}, | ||
] | ||
theoretically(({input, expected}) => `the number ${input} is ${expected ? '' : 'not'} bigger than 1000`, theories, theory => { | ||
const output = IsBiggerThan1000(theory.input); | ||
expect(output).toBe(theory.expected); | ||
}) | ||
}); | ||
``` | ||
## Inspiration | ||
Inspiration from [jasmine-theories](https://github.com/hypesystem/jasmine-theories) and XUnit. |
6419
28.92%54
28.57%55
57.14%