From edc68fea58dd03aef68166f810ec1f009a6b437c Mon Sep 17 00:00:00 2001 From: BilalG1 Date: Fri, 12 Sep 2025 10:41:53 -0700 Subject: [PATCH] test fixes (#893) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## High-level PR Summary This PR improves email testing in the end-to-end test suite by adding a new `waitForMessagesWithSubject` helper method to the `Mailbox` class. This method replaces the previous pattern of using arbitrary wait times (e.g., `wait(2000)`) followed by fetching and finding messages, which could lead to flaky tests. The new approach implements a polling mechanism that waits until messages with the specified subject appear, with a reasonable timeout. The PR updates all email-related tests to use this new helper method, making the tests more reliable and less dependent on timing assumptions. ⏱️ Estimated Review Time: 0h 15m
💡 Review Order Suggestion | Order | File Path | |-------|-----------| | 1 | `apps/e2e/tests/helpers.ts` | | 2 | `apps/e2e/tests/backend/endpoints/api/v1/send-email.test.ts` | | 3 | `apps/e2e/tests/backend/endpoints/api/v1/unsubscribe-link.test.ts` |
---- > [!IMPORTANT] > Improves email test reliability by adding `waitForMessagesWithSubject` in `Mailbox` and updating tests to use it. > > - **Behavior**: > - Introduces `waitForMessagesWithSubject` in `Mailbox` class in `helpers.ts` to replace arbitrary wait times with a polling mechanism. > - Updates email-related tests in `send-email.test.ts` and `unsubscribe-link.test.ts` to use `waitForMessagesWithSubject` for more reliable email verification. > - **Tests**: > - Replaces `wait()` calls with `waitForMessagesWithSubject()` in `send-email.test.ts` and `unsubscribe-link.test.ts`. > - Ensures emails are verified by subject, reducing flakiness in tests. > - **Misc**: > - Minor refactoring in `helpers.ts` to support new functionality. > > This description was created by [Ellipsis](https://www.ellipsis.dev?ref=stack-auth%2Fstack-auth&utm_source=github&utm_medium=referral) for 90b7bdad01fc73edbaa725e08450d0a1b9b4be26. You can [customize](https://app.ellipsis.dev/stack-auth/settings/summaries) this summary. It will automatically update as commits are pushed. ---- ## Review by RecurseML _🔍 Review performed on [d14317c..f42dfc5](https://github.com/stack-auth/stack-auth/compare/d14317c78713e383ef058973ee545ac88eb63d3a...f42dfc53512356aa80c0d170adbc361d919e2705)_ | Severity | Location | Issue | Action | |----------|----------|-------|--------| | ![Medium](https://img.shields.io/badge/Medium-yellow?style=plastic) | [apps/e2e/tests/helpers.ts:235](https://github.com/stack-auth/stack-auth/pull/893#discussion_r2342704515) | Async function call not wrapped with runAsynchronously | [![Dismiss](https://img.shields.io/badge/Dismiss-lightgray?style=plastic)](https://squash-322339097191.europe-west3.run.app/interactive/94b4ba1ab226246ff7102274bc246d6555e6dc4b3287aedac73fe998f4900bc9/?repo_owner=stack-auth&repo_name=stack-auth&pr_number=893) |
✅ Files analyzed, no issues (2) • `apps/e2e/tests/backend/endpoints/api/v1/send-email.test.ts` • `apps/e2e/tests/backend/endpoints/api/v1/unsubscribe-link.test.ts`
[![Need help? Join our Discord](https://img.shields.io/badge/Need%20help%3F%20Join%20our%20Discord-5865F2?style=plastic&logo=discord&logoColor=white)](https://discord.gg/n3SsVDAW6U) ## Summary by CodeRabbit * **Tests** * More reliable email end-to-end tests by waiting for messages by subject instead of fixed delays and manual polling. * Multi-recipient scenarios now independently confirm delivery for each user. * Switched to snapshot-style assertions capturing full message metadata for clearer, more stable verification. * Unsubscribe email tests now wait deterministically for delivery before validating links. * **Chores** * Mailbox handling improved to reduce flakiness and repeated network calls. --- .../endpoints/api/v1/send-email.test.ts | 47 +++++++++---------- .../endpoints/api/v1/unsubscribe-link.test.ts | 11 ++--- apps/e2e/tests/helpers.ts | 18 ++++++- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/apps/e2e/tests/backend/endpoints/api/v1/send-email.test.ts b/apps/e2e/tests/backend/endpoints/api/v1/send-email.test.ts index abc39981a..ebccc89dd 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/send-email.test.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/send-email.test.ts @@ -294,11 +294,22 @@ it("should return 200 and send email successfully", async ({ expect }) => { `); // Verify the email was actually sent by checking the mailbox - await wait(2000); - const messages = await user.mailbox.fetchMessages(); - const sentEmail = messages.find(msg => msg.subject === "Custom Test Email Subject"); - expect(sentEmail).toBeDefined(); - expect(sentEmail!.body?.html).toMatchInlineSnapshot(`"http://localhost:8102/api/v1/emails/unsubscribe-link?code=%3Cstripped+query+param%3E"`); + const messages = await user.mailbox.waitForMessagesWithSubject("Custom Test Email Subject"); + expect(messages).toMatchInlineSnapshot(` + [ + MailboxMessage { + "attachments": [], + "body": { + "html": "http://localhost:8102/api/v1/emails/unsubscribe-link?code=%3Cstripped+query+param%3E", + "text": "http://localhost:8102/api/v1/emails/unsubscribe-link?code=%3Cstripped+query+param%3E", + }, + "from": "Test Project ", + "subject": "Custom Test Email Subject", + "to": ["@stack-generated.example.com>"], +