Created
December 14, 2018 18:47
-
-
Save ismyrnow/ee2c68436b75c0c57b15f174ea9d4c0c to your computer and use it in GitHub Desktop.
Parameterized test helper for Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Setup parameterized tests for a dictionary of inputs and outputs. | |
* This allows you to run tests over a list of inputs and outputs, | |
* without much setup. For example: | |
* | |
* setupParameterizedTests(x => getFirstName(x))([ | |
* ['John M. Smith', 'John'], | |
* ['Joe Schmoe Jr.', 'Joe'] | |
* ]); | |
* | |
* Each case will call `expect().toEqual()` for you, but provide enough | |
* information that test case failures are debuggable using test output. | |
* | |
* @param {Function} fn - function to run on input, which produces output | |
* @param {Array} dictionary - 2D array containing pairs of input/output | |
*/ | |
export default function setupParameterizedTests(fn) { | |
return dictionary => { | |
dictionary.forEach(([input, result]) => { | |
expect({input, result: fn(input)}).toEqual({input, result}); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment