Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
41
rated 0 times [  44] [ 3]  / answers: 1 / hits: 103758  / 7 Years ago, wed, august 16, 2017, 12:00:00

How do I loop through dynamic test cases in Jest?


I have test cases like the following how do I dynamically create jest test case using it/test methods.


Here is what I have tried , However it just passes without excuting the test cases in the loop.


    const mymodule = require('mymodule');

const testCases = [
{q: [2, 3],r: 5},
{q: [1, 2],r: 3},
{q: [7, 0],r: 7},
{q: [4, 4],r: 8}
];

describe("Test my Math module", () => {
test("test add method", () => {
for (let i = 0; i < testCases.length; i++) {
const { q,r } = testCases[i];
it(`should add ${q[0]},${q[1]} to ${expected}`, () => {
const actual = mymodule.add(q[0] + q[1]);
expect(actual).toBe(expected);
});
}
});

});

More From » node.js

 Answers
4

There's an in-built way to do this: test.each(table)(name, fn, timeout)


e.g.


test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
'.add(%i, %i)',
(a, b, expected) => {
expect(a + b).toBe(expected);
},
);

where each inner array in the 2D array is passed as args to the test function.


[#56745] Monday, August 14, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorafrancisl

Total Points: 80
Total Questions: 96
Total Answers: 102

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
;