I have many modules which all have the same function with the same interface exported but each function from each module does something in a slightly different way.
I am using Jest for my unit-test, and I have written a test file for each module, but all the test files look very similar with very similar code as all of them test one function with the same interface.
The setup is something like this:
lib/
module_1.js
module_2.js
module_3.js
...
module_n.js
lib/__tests__/
module_1.js
module_2.js
module_3.js
...
module_n.js
Each test file in lib/__tests__/ is something like this (simplified version):
// lib/__tests__/module_1.js
const func = require('../module_1');
describe('Test cases for module_1', () => {
// this test cases are different for each module
const cases = [
{
testCase: 'foo1',
result: 'bar1',
},
{
testCase: 'foo2',
result: 'bar2',
},
];
// this forEach loop is repeated in all test files.
// How can I refactor this so I don't repeat this same loop in every test file?
cases.forEach(c => {
it('should return something', () => {
expect(func(c.testCase)).toBe(c.result);
});
});
});
Is there a way to refactor this to extract the forEach loop so that it is not repeated over every test file?
Or how can I implement this unit-tests so that if I have to change something someday I don't have to go through every test file changing the same code in all of them?
I tried placing all tests in a single file with an array of arrays for all test cases and then looping through but it is not good solution as it becomes very large and is not obvious which test is which.
Aucun commentaire:
Enregistrer un commentaire