'Running jest tests in parallel based on a list of parameters

In my case I have a test file containing a few hundred tests using jest

describe('my test-suite', () => {
   test('test 1', () => {
       expect(1).toBe(1);
   });
   //another hundred tests...
});

What I need to do now is to run these tests for different parameters, specified on a config file. More in details in my case the config file specifies some external parameters for external services (think like DB access parameters) and ideally I would like to keep exactly the same structure I already have, running the same tests multiple times against all these external services.

Ideally I should be able to do something like:

const paramList = [{
    name: 'service 1'
}, {
    name: 'service 2'
}];

describe('my parallel test-suite', () => {
    it.each(paramList)("testing against $name", ({name}) => {

        //just what I had before
        describe('my test-suite', () => {
           test('test 1', () => {
               expect(1).toBe(1);
           });
           //another hundred tests...
        });

    });
});

Ideally the next step would be to replace the test.each with test.concurrent.each and being able to run my tests in parallel against all the different external services.

Unfortunately with the code (and Jest version 27.4.7) above what I get is:

Cannot nest a describe inside a test. Describe block "my test-suite" cannot run because it is nested within "service service 1".

Tests cannot be nested. Test "test 1" cannot run because it is nested within "service service 1".

Is there a way to achieve what I'm trying to do here?



Solution 1:[1]

You can't run a describe block in an each loop. Run each test in its own loop and then it will work. Something like this:

describe('my parallel test-suite', () => {
    it.each(paramList)("testing against $name", ({name}) => {
        expect(1).toBe(1);
    });
    it.each(paramList)("another test against $name", ({name}) => {
        expect(2).toBe(2);
    });
    //another hundred tests...
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tal weissler