Sorry if I confused you with the title, but here is what I’m facing now after migrating to 0.52 version (actually now on 0.53).
This is my test script example:
await page.waitForLoadState('networkidle');
describe(pageTitle, () => {
describe('lorem-searching: should be able to search for Lorem ipsum', async () => {
expect(
await page.locator('.mailpoet-listing-title').innerText(),
).to.contain('Lorem ipsum');
});
});
and the output is like this:
✓ expected ${this} to include 'Lorem ipsum'
✓ expected ${this} to exist
█ My Page
█ lorem-searching: should be able to search for Lorem ipsum
█ lorem-searching: should be able to see Lorem ipsum
Question: Why the expected text in the output is outside of the describe block?
This was not present before migration. I know that I added await to many places where necessary (almost everywhere). Could it be some ordering is issue here… like what is executed first.
PS showing you only one describe and assertion, but the output shows 2. That’s expected, I’m showing only one describe block as an example.
It would really help if you could supply me with the full test script or a subset of it so that I can recreate it on my side. I can only guess what the issue could be at the moment, maybe something like this would help resolve it?:
await page.waitForLoadState('networkidle');
describe(pageTitle, () => {
// Does this need to be awaited?
await describe('lorem-searching: should be able to search for Lorem ipsum', async () => {
expect(
await page.locator('.mailpoet-listing-title').innerText(),
).to.contain('Lorem ipsum');
});
});
Our current implementation of k6chaijs doesn’t have support for async calls. Under the hood the current describe in k6chaijs works with other APIs which doesn’t support async functions either, and this is preventing us from creating a long term fix to support async functions. For now you have two work arounds:
Move the async calls outside the describe, for example:
await page.waitForLoadState('networkidle');
const ml = await page.locator('.mailpoet-listing-title').innerText();
describe(pageTitle, () => {
describe('lorem-searching: should be able to search for Lorem ipsum', () => {
expect(ml).to.contain('Lorem ipsum');
});
});
The other option you have is to work with an asyncDescribe like the one that is implemented here (it’s not an exported API, so you will have to copy and paste it into your test script). Here’s an example script I created to test it out with:
import { browser } from 'k6/browser';
import {
expect,
} from 'https://jslib.k6.io/k6chaijs/4.5.0.0/index.js';
// Work with the check polyfill that supports async functions.
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
iterations: 1,
options: {
browser: {
type: 'chromium'
},
},
},
},
}
export default async function () {
const page = await browser.newPage();
await page.goto('https://test.k6.io/my_messages.php');
await asyncDescribe("testing", async () => {
await asyncDescribe('h2-search: should be able to search for Unauthorized', async () => {
expect(
await page.locator('h2').innerText(),
).to.contain('Unauthorized');
});
});
await page.close();
}
async function asyncDescribe(description, callback) {
try {
await callback((what) => {
return expect(what, description)
})
} catch (error) {
if (error.name !== 'AssertionError') {
// eslint-disable-next-line no-undef
console.error(`FAIL [${description}]`, error.stack);
check(error, {[description]: () => false})
} else {
// eslint-disable-next-line no-undef
console.error(`FAIL [${description}]`, error.message);
}
}
}
Hi @ankur
Can you please tell me if there is any other alternative library that would work in original way of not using the first option of const outside the describe blocks, or we must stick to the chaijs and k6?
Additionally, do you expect for this to be fixed in some of the next k6 releases?