Jasmine create specs that depend on a variable list size

70 Views Asked by At

I have a beforeAll function, which goes to my webpage and populates a list based on elements from the page. The amount of these elements vary depending on what the users do in the UI.

let variableList = []

beforeAll(() => {populate a list with elements found in the page}

Then, I want to do the same test on all elements of my list, I don't want to write a spec for each element (as these elements may change, and its a very long list).

I don't want to use the list within one unique spec, as it would take me too long to run that spec. I rather have one spec for each component of my list.

variableList.map(element => {
   it('should run a spec for each list element', function(()=>{
     //Using element to perform the tests

   })
})

When running this code, the beforeAll function will not populate the variableList. The list is used without being populated.

Any ideas on how I could have a better approach to this?

Thanks in advance!

1

There are 1 best solutions below

0
On

In Jasmine, code that occurs inside a beforeEach(), afterEach(), beforeAll(), afterAll(), or it() function is only executed when the tests actually run. All other code (including in describe() functions) happens when the test runner starts up. I suggest doing all the work inside a single async test:

it("tests all the elements", done => {
    getElementFromOtherPage().then( elements => {
         element.forEach( runTestOnElement );
         done();
    });
});

Not very elegant but should work correctly.