Open qa-danny opened 1 year ago
Thanks for raising this. Btw, have you checked out our new @nightwatch/apitesting
plugin which has integrated support for supertest and also a built-in mock server?
I changed the nightwatch pause function to a custom one, and it did not fail. You can see in this screenshot there are no "Completed Wait For Expected Response" and "Passed Assertion After Expected Failure In WFER" console comments in this latest test run:
This issue seems to be caused by the pause
command. We will investigate and work to resolve it promptly.
Also, another thing I noticed on debugging is that on not calling the apiMethod()
and setting doesContainExpected
to false it logs "Completed Wait For Expected Response" and "Passed Assertion After Expected Failure In WFER" only after the function/custom command as it is supposed to. Iam not sure why we see this behaviour if pause
is the suspect
async command(apiMethod, expectedResponse, retries = 3, timeout = 1500) {
let response;
let data;
let doesContainExpected = false;
for (; retries > 0; retries--) {
console.log('for loop', new Date());
try {
// response = await apiMethod();
console.log('completed apiMethod()', new Date());
// data = await response.json();
} catch (error) {
await this.api.assert.ok(false, error);
}
// look through all keys in expectedResponse, and check the API response to see if they deepEqual
// doesContainExpected = Object.keys(expectedResponse).every(key => deepEqual(expectedResponse[key], data[key]));
console.log(`Contains Expected Response? ${doesContainExpected}`);
if (doesContainExpected) {
return;
}
console.log('pausing in function', new Date());
await this.api.pause(timeout);
console.log('about to repeat loop', new Date());
}
await this.api.assert.ok(doesContainExpected, `Exceeded max number of retries. API Response ${JSON.stringify(data)} does not contain Expected Response ${JSON.stringify(expectedResponse)}`);
}
Description of the bug/issue
There appears to be an async timing issue with one of our custom functions while using Nightwatch. The custom function calls an API and compares the response with an expected response before continuing on. The failure happens when the expected response does not match, the rest of the test script appears to run while the function goes into a retry loop.
Steps to reproduce
This deals with an async/await inside of a custom function dealing with an API call.
Sample test
Command to run
Verbose Output
Nightwatch Configuration
Nightwatch.js Version
2.6.10
Node Version
16.13.1
Browser
Chrome 108
Operating System
MacOS Monterey 12.6.2
Additional Information
Screen shot of console with timestamps, highlighted line is a console.log command printing out AFTER the function that contains the loop that is still running through.
I created a dummy project for this, and it is located here: https://github.com/qa-danny/nw_test