it
doesn't work inside forEach
loop
#24345
-
Can anyone help me understand why doesn't the test inside the describe("all the links", () => {
let allLinks = [];
before(() => {
cy.task('readSitemapLinks').then((links: string[]) => {
allLinks = allLinks.concat(links);
});
});
it("should have 100 links", () => { // This test runs
expect(allLinks.length).to.eq(100);
})
allLinks.forEach((link) => {
it('the link should work', () => { // These tests never run
cy.request(link ? link : '/').then(({ status }) => {
expect(status).to.eq(200);
});
});
});
}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
but for (let i = 0; i < 3; i++) {
describe("something", ()=> {
it("some tests", () => {})
})
} |
Beta Was this translation helpful? Give feedback.
-
you are running into the asynchronicity issue. think of you can easily fix this issue by moving your |
Beta Was this translation helpful? Give feedback.
you are running into the asynchronicity issue. think of
before()
block as a part of yourit()
blocks. basically, the reason your tests aren’t running is because you are generating your tests using empty array. that array does not get filled untilbefore()
hook runs. but sincebefore()
hook is essentially a part of yourit()
, those tests will never get generated.you can easily fix this issue by moving your
allLinks.forEach()
function inside yourit()
block