fix(dataExport): Handle empty emails array TypeError#39186
fix(dataExport): Handle empty emails array TypeError#39186Harshit2405-2004 wants to merge 3 commits intoRocketChat:developfrom
Conversation
Closes RocketChat#39185 Adds safe optional chaining to user.emails[0].address in sendViaEmail.ts to prevent throwing TypeError when the array is explicitly empty.
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
🦋 Changeset detectedLatest commit: 99fc95a The changes in this PR will be included in the next version bump. This PR includes changesets to release 41 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughAdds a unit test for sendViaEmail and fixes a TypeError by using safe optional chaining when reading a user's first email address, preventing crashes when a user's Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts (2)
74-77: Remove inline comment per coding guidelines.The comment on line 76 can be removed. The test name already communicates the intent, and the assertion pattern makes the expectation clear.
🧹 Proposed fix
const executorUser = { _id: 'admin', username: 'admin', emails: [{ address: 'admin@example.com' }] }; - // Should NOT throw TypeError const result = await sendViaEmail(reqData, executorUser as any);As per coding guidelines: "Avoid code comments in the implementation"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts` around lines 74 - 77, Remove the inline comment "// Should NOT throw TypeError" that sits immediately before the invocation of sendViaEmail in the test; the test name and assertion already communicate the expectation. Edit the test around the invocation of sendViaEmail(reqData, executorUser as any) to delete that comment and leave the executorUser and reqData setup and the result assignment intact.
37-57:mockMomentappears unused.The
mockMomentfunction defined on lines 37-41 is not referenced in the proxyquire setup. Instead,getMomentLocaleis mocked directly. Consider removingmockMomentif it's not needed.🧹 Proposed fix to remove unused code
-const mockMoment = function () { - return { - locale: () => ({ format: () => '12:00' }), - }; -}; - const { sendViaEmail } = proxyquire.load('./sendViaEmail.ts', {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts` around lines 37 - 57, The mockMoment function declared as mockMoment is unused in this test — remove its definition (the const mockMoment = function() { ... }) and any unused imports related to it; if you intended to stub moment via the proxyquire.load call for sendViaEmail, instead inject it by replacing the '../getMomentLocale' stub with a module that returns mockMoment (i.e., ensure proxyquire.load for './sendViaEmail.ts' references getMomentLocale or delete mockMoment and leave the current getMomentLocale stub as-is).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts`:
- Around line 74-77: Remove the inline comment "// Should NOT throw TypeError"
that sits immediately before the invocation of sendViaEmail in the test; the
test name and assertion already communicate the expectation. Edit the test
around the invocation of sendViaEmail(reqData, executorUser as any) to delete
that comment and leave the executorUser and reqData setup and the result
assignment intact.
- Around line 37-57: The mockMoment function declared as mockMoment is unused in
this test — remove its definition (the const mockMoment = function() { ... })
and any unused imports related to it; if you intended to stub moment via the
proxyquire.load call for sendViaEmail, instead inject it by replacing the
'../getMomentLocale' stub with a module that returns mockMoment (i.e., ensure
proxyquire.load for './sendViaEmail.ts' references getMomentLocale or delete
mockMoment and leave the current getMomentLocale stub as-is).
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts
📜 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). (1)
- GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{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/server/lib/dataExport/sendViaEmail.spec.ts
**/*.spec.ts
📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
**/*.spec.ts: Use descriptive test names that clearly communicate expected behavior in Playwright tests
Use.spec.tsextension for test files (e.g.,login.spec.ts)
Files:
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts
🧠 Learnings (15)
📚 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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.ts
📚 Learning: 2025-12-10T21:00:54.909Z
Learnt from: KevLehman
Repo: RocketChat/Rocket.Chat PR: 37091
File: ee/packages/abac/jest.config.ts:4-7
Timestamp: 2025-12-10T21:00:54.909Z
Learning: Rocket.Chat monorepo: Jest testMatch pattern '<rootDir>/src/**/*.spec.(ts|js|mjs)' is valid in this repo and used across multiple packages (e.g., packages/tools, ee/packages/omnichannel-services). Do not flag it as invalid in future reviews.
Applied to files:
apps/meteor/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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 **/*.spec.ts : Use descriptive test names that clearly communicate expected behavior in Playwright tests
Applied to files:
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts
📚 Learning: 2025-10-06T20:30:45.540Z
Learnt from: d-gubert
Repo: RocketChat/Rocket.Chat PR: 37152
File: packages/apps-engine/tests/test-data/storage/storage.ts:101-122
Timestamp: 2025-10-06T20:30:45.540Z
Learning: In `packages/apps-engine/tests/test-data/storage/storage.ts`, the stub methods (updatePartialAndReturnDocument, updateStatus, updateSetting, updateAppInfo, updateMarketplaceInfo) intentionally throw "Method not implemented." Tests using these methods must stub them using `SpyOn` from the test library rather than relying on actual implementations.
Applied to files:
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts
📚 Learning: 2025-12-16T17:29:45.163Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 37834
File: apps/meteor/tests/e2e/page-objects/fragments/admin-flextab-emoji.ts:12-22
Timestamp: 2025-12-16T17:29:45.163Z
Learning: In page object files under `apps/meteor/tests/e2e/page-objects/`, always import `expect` from `../../utils/test` (Playwright's async expect), not from Jest. Jest's `expect` has a synchronous signature and will cause TypeScript errors when used with web-first assertions like `toBeVisible()`.
Applied to files:
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts
📚 Learning: 2026-02-24T19:22:48.358Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 38493
File: apps/meteor/tests/e2e/omnichannel/omnichannel-send-pdf-transcript.spec.ts:66-67
Timestamp: 2026-02-24T19:22:48.358Z
Learning: In Playwright end-to-end tests (e.g., under apps/meteor/tests/e2e/...), prefer locating elements by translated text (getByText) and ARIA roles (getByRole) over data-qa attributes. If translation values change, update the corresponding test locators accordingly. Never use data-qa locators. This guideline applies to all Playwright e2e test specs in the repository and helps keep tests robust to UI text changes and accessible semantics.
Applied to files:
apps/meteor/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.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/server/lib/dataExport/sendViaEmail.spec.ts
🔇 Additional comments (6)
apps/meteor/server/lib/dataExport/sendViaEmail.spec.ts (6)
1-3: LGTM!Appropriate testing library imports for a unit test with module isolation.
5-30: LGTM!The mock correctly covers the three user email scenarios: valid email, empty array, and undefined. The cursor-like structure with
toArray()appropriately mimics the model's return type.
32-35: LGTM!Minimal and effective mailer mock with a stub that enables call verification.
37-41: LGTM!Simple moment mock sufficient for dependency isolation.
59-62: LGTM!Proper test isolation with
beforeEachresetting stub history.
64-87: Well-structured test covering the edge case.The test effectively validates that:
- Users with empty or undefined emails are correctly reported in
missing- Valid users are processed correctly
- The mailer is called with the combined recipient list
This directly addresses the PR objective of preventing TypeError when users have empty emails arrays.
Closes #39185.
Proposed Changes- Adds safe optional chaining to prevent Data Export emails .forEach loop crashing synchronously due to a TypeError if a user has an explicitly empty emails array.
Summary by CodeRabbit
Tests
Bug Fixes