Maxim Koev
Maxim Koev

Reputation: 41

Testcafe can't find tests wrapped in iterator

I have few expand elements in my FAQ page, so I want to test all of them. When writing my fixture and/or test in foreach section, terminal reflects an error ERROR no test to run. I don't understand why

I tried to wrap the only test in foreach and all fixture with the test, the result is the same.

fixture('check FAQ expand items').page(URL.local.faq)//.meta({ status: 'indev' })
  .beforeEach(async () => {await waitForReact();
  })
  .before(async t => {
  await waitForReact();
  await list();
  }).only;
faqItems.forEach( (element) => {
  test(`check that ${element.getReact(({key})=>key)} is present`, async t => {
    await t.expect(element.find('[expanded={true]]').exists).ok();
  });
});

I expect test cafe will run faqItems.length quantity of tests into 'check FAQ expand items' fixture

I run tests with command testcafe chrome faq.test.ts

screen shot of run test result

UPD

let faqItems: Array<Selector> = [faq.item];
async function list() {
  const count =  await faq.item.count;
  console.log(`count = ${count}`)
  for (let i = 0; i < count; i++) {
    await faqItems.push( await faq.item.nth(i));
    console.log(await faqItems[i].getReact(({key})=> key));
  }
}

fixture('check FAQ expand items').page(URL.local.faq)//.meta({ status: 'indev' })
  .beforeEach(async () => {
    await waitForReact();
    await list();
  })
faqItems.forEach((element)=>{
  test(`check that ${element.getReact(({key})=>key)} is present`, async t => {
    await t.expect(element.find('[expanded={true}]').exists).ok();
  });
})

result pic

Upvotes: 4

Views: 725

Answers (2)

Maxim Koev
Maxim Koev

Reputation: 41

The problem was that array initialized empty. I don't know why but into test array contains initialized data instead of generated in "BeforeAll". It seems like a testcafe bug, but maybe I am too junior to understand root cause. p.s. by the way it does not matter where is fixture inside/outside loop.

Upvotes: 0

RedVarVar
RedVarVar

Reputation: 572

First of all, you pre-condition part of code is not part of forEach loop, and that can cause of you a trouble. I consider to move faqItems.forEach() above fixture like that

faqItems.forEach( (element) => {
   fixture('check FAQ expand items').page(URL.local.faq)//.meta({ status: 'indev' })
     .beforeEach(async () => {await waitForReact();
     })
     .before(async t => {
        await waitForReact();
        await list();
     }).only;

     test(`check that ${element.getReact(({key})=>key)} is present`, async t => {
        await t.expect(element.find('[expanded={true]]').exists).ok();
     });
   });
})

If in fixture you have some test which shouldn't run for each FAQ items, then move it to another fixture.

Second, i am not sure, that you can use forEach here, because forEach as a loop and as function for Arrays is not working with async/await. You can use for...of loop or standard for loop

Upvotes: 3

Related Questions