-1

I use a cartesian combination method to generate combos of test cases from an array.

const cartesian = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));

Found in this answer.

let arrayOne = [0, 1];
let arrayTwo = [0, 1];

const result = cartesian(arrayOne, arrayTwo)

console.log(result)

/**
* Result should equal:
* [
* [0, 1],
* [1, 0],
* [1, 1],
* [0, 0] 
* ]
*/

That part is working nicely.

I then take the array of arrays generated from this method and pass them to Jest's .each method on the test.

describe('test', () => {
  test.each(cases)('when one equals %s and two equals %s', (one, two) => {
    // my test logic
  })
})

This also works nicely. However, when I have a very large number of combinations I want to test,

e.g., [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ...]

that's when things get tricky.

I don't think JavaScript is built to handle that level of computational complexity. I usually get a heap out of memory when I run through so many cases in a single test.

This is a shame, because this method is quite convenient for automating test case building, and I really would like to expand it to other languages (like PHP and Python), but I think I'll have the same problem once my test cases pass the 100,000 mark.

One method I've tried is limiting the number of test cases to an arbitrary number.

describe('test', () => {
  test.each(cases.slice(0,5000))('when one equals %s and two equals %s', (one, two) => {
    // my test logic
  })
})

This lets me run them without a problem, but I can't see all the cases without manually commenting out values in the arrays I've already tested.

const arrayOne = [
  // 0
  1
  ...
]

Doing that every time doesn't seem right.

Is there some way of making the test:

  1. Go through 50,000 records
  2. Save their results
  3. Clear the memory
  4. Start again from the latest index

I think that might be how it's done.

Daniel Haven
  • 156
  • 1
  • 12

1 Answers1

0

Why not simple loop through the tests one by one? The test is Typescript/JavaScript, so do your testing in a loop.

describe('test', () => {
    for await (const case of cases) { // Doing the await in case you need async
        test('should ...', () => {
           // my test logic
        });
    }
});
Steven Scott
  • 10,234
  • 9
  • 69
  • 117