Skip to content

Instantly share code, notes, and snippets.

@ismyrnow
Created December 14, 2018 18:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismyrnow/ee2c68436b75c0c57b15f174ea9d4c0c to your computer and use it in GitHub Desktop.
Save ismyrnow/ee2c68436b75c0c57b15f174ea9d4c0c to your computer and use it in GitHub Desktop.
Parameterized test helper for Jest
/**
* 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