Rayrun
← Back to Discord Forum

test passes with both toBeTruthy() and toBeFalsy() assertions

//test to check if the getStatesForRegions response body includes the duplicate states
    test.only('should not include duplicate states in the /getStatesForRegions response body ',
        async ({page}) => {
            test.slow();
            await page.route('**/getStatesForRegions', async (route) => {
                // Fetch original response.
                const response = await page.request.fetch(route.request());
                // Add a prefix to the title.
                let json = ['A', 'A'];//await response.json();
                //check if the response body includes the duplicate states
                expect(hasDuplicates(json)).toBeFalsy();//HERE
            });
            //select austria and belgium
            await page.locator('#select-country').selectOption(['Austria', 'Belgium']);
            await page.locator('.search').click();
    });

hasDuplicates

//not the best but just meant for tests
function hasDuplicates(arr: string[]) {
    const set = new Set(arr);
    return set.size < arr.length;
}

Why is this test passing even if I switch the assertion to .toBeTruthy()?

This thread is trying to answer question "Why is this test passing even if I switch the assertion to .toBeTruthy()?"

6 replies
rui.figueira
rui.figueira

that route callback is not being waited, therefore is not garanteed the expect call is performed inside the test

rui.figueira
rui.figueira
test('route', async ({page, }) => {
  await page.route('**/index', async (route) => {
    await route.fulfill({ body: '<a href="/search">search</a>' });
  });
  const searchPromise = new Promise<[string, string]>(async resolve => {
    await page.route('**/search', async (route) => {
      await route.fulfill({ body: '<h1>no results</h1>' });
      resolve(['A', 'B']);
    });
  });
  await page.goto('http://localhost:8080/index');
  await page.locator('a').click();
  const [first, second] = await searchPromise;
  expect(first === second).toBeFalsy();
});
rui.figueira
rui.figueira

the important part there is the searchPromise

rui.figueira
rui.figueira

and the call to resolve(...)

rui.figueira
rui.figueira

then you wait for that result to prevent the test from completing before you receive it

Thank you very much, the searchPromise part solves my issue

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].