A Shelf test checks that returning a book clears the late fee. The feature breaks in a release. The test stays green for three weeks, until a reader is charged for a book they returned.
You have now seen this problem four times in four shapes. A date was read before the timer changed it. A check ran on a fixed pause before the save was done. A then callback ran after the line below it. A helper returned a promise and the caller printed the promise. Each time, a value was read before the work finished.
In test code, all four have the same cause and the same name: a missing await.
let fee = 4;
function returnBook() {
return new Promise((resolve) => {
setTimeout(() => {
fee = 0;
resolve();
}, 20);
});
}
async function testReturnClearsFee() {
returnBook();
if (fee !== 0) {
throw new Error("The fee is still " + fee);
}
}
testReturnClearsFee()
.then(() => console.log("test passed"))
.catch((error) => console.log("test failed: " + error.message));
test failed: The fee is still 4
returnBook() returns a promise and nobody waits for it. The check runs at once and sees the old fee. Here the feature works and the test fails. That is the lucky version, because a red test gets looked at.
The unlucky version
Now move the missing await to the check. In Playwright, a check on the page returns a promise too. This example builds a small expectFee that behaves the same way.
let fee = 4;
function expectFee(expected) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (fee === expected) {
resolve();
} else {
reject(new Error("Expected a fee of " + expected + " and found " + fee));
}
}, 20);
});
}
async function testReturnClearsFee() {
expectFee(0).catch(() => {});
}
testReturnClearsFee()
.then(() => console.log("test passed"))
.catch((error) => console.log("test failed: " + error.message));
test passed
The fee is 4 and the test passes. expectFee(0) starts the check and returns a promise. The test function reaches its end without waiting, so the test runner records a pass. The check fails 20 milliseconds later, after the result is already written down.
The .catch(() => {}) is there only to keep this example quiet. In a real project the late failure shows up as an "unhandled rejection" warning somewhere in the log, or as a failure reported against the next test, or not at all.
With await in front of expectFee(0), the test waits and fails as it should:
let fee = 4;
function expectFee(expected) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (fee === expected) {
resolve();
} else {
reject(new Error("Expected a fee of " + expected + " and found " + fee));
}
}, 20);
});
}
async function testReturnClearsFee() {
await expectFee(0);
}
testReturnClearsFee()
.then(() => console.log("test passed"))
.catch((error) => console.log("test failed: " + error.message));
test failed: Expected a fee of 0 and found 4
Where it hides
Look in three places.
- On an action.
page.click(...)withoutawait. The next line runs before the click has happened. - On a check.
expect(locator).toHaveText(...)withoutawait. The test can end before the check gives its answer. This is the one that passes on a broken feature. - On your own helper. A helper marked
asyncis called withoutawait. Everything inside it is correct, and the caller does not wait for any of it.
There is one more signal. A test that fails only when it runs alone, or only when it runs last, often has a check that finishes after the test did.
Tools can find most of these for you. The lint rule no-floating-promises reports a promise that is neither awaited nor returned. Until that rule is on in your project, read every line that touches the page and look for the await.
Remember this
A promise that nobody awaits still runs, and nothing waits for its result. On an action, the next line runs too early. On a check, the test can pass while the check fails. Every call that returns a promise needs an await, including your own helpers.
Reference: async function on MDN.