TypeScript Inbox Polling Needs a Stop Rule
Email verification tests often look simple in a ticket: submit the form, wait for a message, click the link. The trouble starts when “wait” becomes an unbounded loop. A message can arrive late, a previous test can be in the same inbox, or a provider can return the same newest message again. In Node.js test harnesses, I prefer treating an inbox as a small event stream with an explicit stop rule.…
When email verification tests are executed, they typically follow a simple pattern: submit the form, wait for a message to arrive, and then click the provided link. However, this straightforward approach can become problematic when the "wait" period turns into an unbounded loop. Various factors can contribute to this issue, such as delayed message arrivals, overlapping tests accessing the same inbox, or the provider returning the same newest message repeatedly.
To address these challenges, I recommend treating the inbox as a small event stream with a clear stop rule. This approach enhances test speed when successful, simplifies failure diagnosis, and ensures TypeScript code accurately distinguishes between "not found yet," "found," and "test cannot continue." Polling is useful because email delivery is eventually consistent.
Sometimes, the API that sends the verification message may finish before the inbox reader can access the message. Therefore, a short delay between reads is normal. The problem arises when you use a loop like this: while (! (await hasVerificationMessage())) { await sleep(1000); } This type of loop lacks a deadline, does not keep track of previously inspected messages, and provides no meaningful error if the message never arrives.
In continuous integration (CI) environments, a blocked worker can make the entire job appear hung. Implementing a retry count is a viable solution, but a time budget conveys the intent more clearly: this test is willing to wait until a specific deadline, after which it must produce a failure receipt. For browser suites, it's also beneficial to separate the delivery window from the UI wait.
In Playwright email tests, delivery windows and mailbox timeouts should not quietly become the same setting. To model the inbox as a run-scoped stream, create a run identity and capture the newest message that you already know about. This message serves as the starting cursor. The test should only accept a later message that matches the expected recipient, subject pattern, and run marker.
The key components needed for this approach are the MailMessage and InboxCursor interfaces. The MailMessage type includes properties such as id, receivedAt, to, subject, and text. The InboxCursor type consists of a runId and an optional afterMessageId. To prevent matching on generic subject lines, such as "Verify your email," which can lead to old messages passing new tests, it's crucial to match on more specific criteria.
For instance, use a run-scoped inbox to avoid common debugging issues where one test consumes a token intended for another. If the system cannot create a separate inbox for each run, record the cursor and make the matching rules stricter. While some setup effort is involved, it's much cheaper than guessing which message arrived first.
When designing a small TypeScript polling contract, it's essential to keep the polling function independent from Playwright or any other UI runner. This polling function should receive a message reader and return a clear result, making the mail behavior testable without opening a browser. The PollResult type is a union with two variants: found and timeout.
The found variant contains the expected mail message, while the timeout variant provides the inspected count, last checked timestamp, and allows the caller to attach relevant information like the run ID, recipient, elapsed time, and the last observed message IDs to the test report. This context can significantly save time during post-failure analysis, especially when failures occur overnight.
Lastly, selecting a cursor and timeout together is crucial. A cursor without a timeout still allows for stuck tests, while a timeout without a cursor can still result in matching stale messages. It's important to pick both as part of one contract. The appropriate values for these parameters will depend on the specific mail provider and CI environment.
Begin with a generous delivery window and then measure actual delivery times before tightening the settings.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to