In this episode, we dive into the tricky world of race conditions in end-to-end testing, particularly when using frameworks like Inertia. We'll look at a classic situation: testing the cancellation of a task deletion. The core issue here is that sometimes your test's assertion runs before your app has actually finished doing the thing you're testing, leading to false positives or negatives.
We start by setting up a test that should ensure cancelling the delete action keeps the task around. At first, the test seems to pass—even when there's a bug, and the task actually gets deleted! This leads us into a discussion of why that's happening: the test is asserting before the app has dealt with the interaction. Nothing visually changes on the page when "Cancel" is clicked, so we can't just wait for DOM updates.
The solution? We use a nifty trick: waitForLocation
. This tells the test to wait for the next navigation event (like being redirected back to the task list) before running assertions. Once we add this, the test starts behaving as expected, catching errors properly. We also mention that methods like arbitrary pauses can work, but they're much less reliable.
Overall, it's all about making sure your test waits for the right moment—especially when the UI doesn’t give any obvious clues. Getting these details right is what makes for robust, trustworthy tests!