Skip to content

test: log last request body/query/response on test or hook failure#40345

Open
KevLehman wants to merge 2 commits intodevelopfrom
improve/api-test-fail-output
Open

test: log last request body/query/response on test or hook failure#40345
KevLehman wants to merge 2 commits intodevelopfrom
improve/api-test-fail-output

Conversation

@KevLehman
Copy link
Copy Markdown
Member

@KevLehman KevLehman commented Apr 30, 2026

Proposed changes (including videos or screenshots)

Issue(s)

Steps to test or reproduce

Further comments

Logs for API fails would be now:

{
  where: '[ABAC] (Enterprise Only) "before all" hook in "[ABAC] (Enterprise Only)"',
  type: 'hook',
  lastUrl: '/api/v1/abac/attributes',
  lastMethod: 'get',
  lastBody: undefined,
  lastQuery: undefined,
  lastResponse: '{"success":false,"error":"You must be logged in to do this.","status":"error","message":"You must be logged in to do this."}'
}

which gives more info on what happened.

It's a small change for normal tests (it) because we had some logging around those. However, if the failure was on a before/after hook, we didn't have the visibility and the current approach (an afterEach) didn't work for this specific case.

Summary by CodeRabbit

  • Tests
    • Improved end-to-end test diagnostics with a new failure reporter that captures and prints the last request and response when a test fails.
    • Enhanced request capturing during tests (URL, method, body, query, and response) and exposed an accessor to retrieve that context for better debugging.

@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented Apr 30, 2026

Looks like this PR is ready to merge! 🎉
If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

⚠️ No Changeset found

Latest commit: 358b614

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9ff7e81b-8ab2-4cfe-959d-bea3ad3d06a8

📥 Commits

Reviewing files that changed from the base of the PR and between bc698c3 and 358b614.

📒 Files selected for processing (1)
  • apps/meteor/tests/end-to-end/teardown.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/meteor/tests/end-to-end/teardown.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: ⚙️ Variables Setup
  • GitHub Check: CodeQL-Build
  • GitHub Check: CodeQL-Build

Walkthrough

Adds a custom Mocha reporter and refactors test teardown to expose last-request details via an accessor; the reporter queries that accessor on test failures and logs structured request/response data to stdout.

Changes

Cohort / File(s) Summary
Mocha config & Reporter
apps/meteor/.mocharc.api.js, apps/meteor/tests/end-to-end/reporter.ts
Adds reporter: 'tests/end-to-end/reporter.ts' to Mocha config and implements FailDumpReporter that listens for test-fail events and logs structured failure data (test title/type, last request URL, method, body, query, response text).
Request tracking / teardown
apps/meteor/tests/end-to-end/teardown.ts
Replaces failure-only console logging with exported getLastRequest() accessor. Tracks last request URL/method plus lastBody, lastQuery, and lastResponse captured from supertest wrappers; no direct console output in teardown.

Sequence Diagram(s)

sequenceDiagram
    participant Runner as Mocha Runner
    participant Reporter as FailDumpReporter
    participant Tracker as Request Tracker

    Runner->>Runner: Execute tests
    Runner->>Reporter: Emit EVENT_TEST_FAIL (failing test)
    Reporter->>Tracker: Call getLastRequest()
    Tracker-->>Reporter: Return { lastUrl, lastMethod, lastBody, lastQuery, lastResponse }
    Reporter->>Reporter: Format and write structured failure log to stdout
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

type: chore

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding logging of request body/query/response when tests or hooks fail. It directly reflects the core functionality introduced across all three modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.81%. Comparing base (3d3970a) to head (358b614).
⚠️ Report is 3 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #40345      +/-   ##
===========================================
- Coverage    69.94%   69.81%   -0.13%     
===========================================
  Files         3297     3298       +1     
  Lines       119267   119292      +25     
  Branches     21515    21547      +32     
===========================================
- Hits         83416    83288     -128     
- Misses       32548    32699     +151     
- Partials      3303     3305       +2     
Flag Coverage Δ
e2e 59.70% <ø> (-0.13%) ⬇️
e2e-api 46.22% <ø> (-0.86%) ⬇️
unit 70.58% <ø> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KevLehman KevLehman marked this pull request as ready for review April 30, 2026 17:28
@KevLehman KevLehman requested a review from a team as a code owner April 30, 2026 17:28
@KevLehman KevLehman added the stat: QA assured Means it has been tested and approved by a company insider label Apr 30, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/meteor/tests/end-to-end/reporter.ts`:
- Around line 12-20: The test reporter currently logs raw payloads (lastBody,
lastQuery, lastResponse?.text) which may contain secrets; update the logging
block that builds the console object (around runnable.fullTitle()) to first
sanitize and truncate these values: for objects (lastBody, lastQuery) walk keys
and replace sensitive keys (e.g., password, token, authorization, apiKey,
access_token, refresh_token) with "[REDACTED]", and for strings (including
lastResponse?.text) truncate to a safe length (e.g., 200 chars) and redact any
bearer/auth patterns; then log the sanitized/truncated versions instead of the
originals so no credentials or long payloads are emitted to CI.

In `@apps/meteor/tests/end-to-end/teardown.ts`:
- Line 11: lastResponse is retained across requests causing stale dumps; reset
it for each request by initializing it to undefined (or null) and clearing it at
the start of every request and in the teardown path. Locate the top-level
variable lastResponse and where requests are issued (test helpers / request
wrapper functions) and set lastResponse = undefined before initiating each
request; also ensure the teardown/afterEach branch that reads lastResponse
clears it after reporting. This ensures lastResponse never points to a prior
response when an error occurs during a new request.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 701d9144-5f2f-4858-953c-3b5ddb635562

📥 Commits

Reviewing files that changed from the base of the PR and between fac6472 and bc698c3.

📒 Files selected for processing (3)
  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/reporter.ts
  • apps/meteor/tests/end-to-end/teardown.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
🧠 Learnings (15)
📓 Common learnings
Learnt from: smirk-dev
Repo: RocketChat/Rocket.Chat PR: 39625
File: apps/meteor/app/api/server/v1/push.ts:85-97
Timestamp: 2026-03-14T14:58:58.834Z
Learning: In RocketChat/Rocket.Chat, the `push.token` POST/DELETE endpoints in `apps/meteor/app/api/server/v1/push.ts` were already migrated to the chained router API pattern on `develop` prior to PR `#39625`. `cleanTokenResult` (which strips `authToken` and returns `PushTokenResult`) and `isPushTokenPOSTProps`/`isPushTokenDELETEProps` validators already exist on `develop`. PR `#39625` only migrates `push.get` and `push.info` to the chained pattern. Do not flag `cleanTokenResult` or `PushTokenResult` as newly introduced behavior-breaking changes when reviewing this PR.
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Utilize Playwright fixtures (`test`, `page`, `expect`) for consistency in test files
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Use `test.step()` for complex test scenarios to improve organization in Playwright tests
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Use `expect` matchers for assertions (`toEqual`, `toContain`, `toBeTruthy`, `toHaveLength`, etc.) instead of `assert` statements in Playwright tests
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Ensure tests run reliably in parallel without shared state conflicts

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Group related tests in the same file

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : All test files must be created in `apps/meteor/tests/e2e/` directory

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Utilize Playwright fixtures (`test`, `page`, `expect`) for consistency in test files

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Maintain test isolation between test cases in Playwright tests

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Ensure clean state for each test execution in Playwright tests

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Use `test.beforeAll()` and `test.afterAll()` for setup/teardown in Playwright tests

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Use `test.step()` for complex test scenarios to improve organization in Playwright tests

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.{ts,spec.ts} : Store commonly used locators in variables/constants for reuse

Applied to files:

  • apps/meteor/.mocharc.api.js
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.{ts,spec.ts} : Follow Page Object Model pattern consistently in Playwright tests

Applied to files:

  • apps/meteor/.mocharc.api.js
  • apps/meteor/tests/end-to-end/teardown.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Use `expect` matchers for assertions (`toEqual`, `toContain`, `toBeTruthy`, `toHaveLength`, etc.) instead of `assert` statements in Playwright tests

Applied to files:

  • apps/meteor/tests/end-to-end/teardown.ts
📚 Learning: 2025-11-24T17:08:17.065Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat PR: 0
File: .cursor/rules/playwright.mdc:0-0
Timestamp: 2025-11-24T17:08:17.065Z
Learning: Applies to apps/meteor/tests/e2e/**/*.spec.ts : Prefer web-first assertions (`toBeVisible`, `toHaveText`, etc.) in Playwright tests

Applied to files:

  • apps/meteor/tests/end-to-end/teardown.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/tests/end-to-end/teardown.ts
  • apps/meteor/tests/end-to-end/reporter.ts
🔇 Additional comments (1)
apps/meteor/.mocharc.api.js (1)

13-13: Reporter configuration change looks good.

Pointing Mocha to the custom reporter here matches the goal of surfacing failure context for both tests and hooks.

Comment thread apps/meteor/tests/end-to-end/reporter.ts
Comment thread apps/meteor/tests/end-to-end/teardown.ts Outdated
@KevLehman KevLehman added this to the 8.5.0 milestone Apr 30, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label Apr 30, 2026
Copy link
Copy Markdown
Member

@cardoso cardoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dionisio-bot dionisio-bot Bot added this pull request to the merge queue Apr 30, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 30, 2026
@dionisio-bot dionisio-bot Bot added this pull request to the merge queue Apr 30, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 30, 2026
@dionisio-bot dionisio-bot Bot added this pull request to the merge queue Apr 30, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat: QA assured Means it has been tested and approved by a company insider stat: ready to merge PR tested and approved waiting for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants