Skip to content

Instantly share code, notes, and snippets.

@takahirohonda
Created October 13, 2021 05:36
Show Gist options
  • Save takahirohonda/9b3f64c03a6c5482a7fa7177552441d4 to your computer and use it in GitHub Desktop.
Save takahirohonda/9b3f64c03a6c5482a7fa7177552441d4 to your computer and use it in GitHub Desktop.
jest-data-provider-pattern
const getAnimal = (sound) => {
switch(sound) {
case 'meow':
return 'Cat';
case 'woof':
return 'Dog';
case 'pawoo':
return 'Elephant';
default:
return 'Human';
}
};
const animalDataProvider = [
{
input: 'meow',
expectedOutput: 'Cat'
},
{
input: 'woof',
expectedOutput: 'Dog'
},
{
input: 'pawoo',
expectedOutput: 'Elephant'
},
{
input: 'whatever',
expectedOutput: 'Human'
},
];
describe.each(animalDataProvider)('getAnimal', (data) => {
it(`should return correct animal with input '${data.input}'`, () => {
console.log(data.input);
const animal = getAnimal(data.input);
expect(animal).toEqual(data.expectedOutput);
} )
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment