Skip to content

Comments

fix: remove redundant non-null assertions#3220

Merged
arkid15r merged 5 commits intoOWASP:mainfrom
anukalp2804:fix/remove-redundant-non-null-assertions
Jan 7, 2026
Merged

fix: remove redundant non-null assertions#3220
arkid15r merged 5 commits intoOWASP:mainfrom
anukalp2804:fix/remove-redundant-non-null-assertions

Conversation

@anukalp2804
Copy link
Contributor

Proposed change

Resolves #3171

This PR removes redundant non-null assertions in the ProjectsDashboardDropDown test file. The values are already guaranteed to exist, so the assertions were unnecessary. This improves readability and avoids redundant TypeScript assertions.

Checklist

  • I read and followed the contributing guidelines
  • I ran local checks (lint + TypeScript + full Jest test suite) and all tests passed
  • I used AI for code, documentation, or tests in this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 6, 2026

Summary by CodeRabbit

  • Tests
    • Improved test robustness and readability by removing unnecessary null assertions in component interaction tests and normalizing a map-test configuration value.

Note: No user-facing changes in this release.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

Removed redundant non-null assertion operators (!) from two frontend test files and normalized a numeric literal in a map test. Test interactions now pass elements directly to fireEvent calls; maxBoundsViscosity changed from 1.0 to 1.

Changes

Cohort / File(s) Summary
Projects dashboard tests
frontend/__tests__/unit/components/ProjectsDashboardDropDown.test.tsx
Removed redundant ! non-null assertions from activeItem and inactiveItem in fireEvent.click() and fireEvent.keyDown() calls.
Chapter map tests
frontend/__tests__/unit/components/ChapterMap.test.tsx
Removed redundant ! non-null assertion from overlay in fireEvent.click() calls and changed maxBoundsViscosity from 1.0 to 1.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • kasya
  • arkid15r

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive Changes in ChapterMap.test.tsx include updating maxBoundsViscosity from 1.0 to 1 and removing non-null assertions, but only the non-null assertion removal directly relates to the linked issue. Clarify whether the maxBoundsViscosity change from 1.0 to 1 is related to satisfying a Sonar rule or is an unrelated code style change that should be in a separate PR.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: remove redundant non-null assertions' directly and accurately describes the main change in the PR.
Description check ✅ Passed The description is directly related to the changeset, explaining the purpose of removing non-null assertions and referencing the linked issue.
Linked Issues check ✅ Passed The PR successfully addresses issue #3171 by removing redundant non-null assertions across test files as required, satisfying the Sonar rule TypeScript:S4325.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 107460b and 497a985.

⛔ Files ignored due to path filters (1)
  • frontend/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/ChapterMap.test.tsx
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: rudransh-shrivastava
Repo: OWASP/Nest PR: 2178
File: frontend/src/app/snapshots/[id]/page.tsx:0-0
Timestamp: 2025-09-21T17:04:48.154Z
Learning: User rudransh-shrivastava confirmed that suggested type safety improvements during Apollo Client migration were no longer relevant, reinforcing their preference to keep migration PRs focused on core migration changes rather than additional improvements.
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:427-427
Timestamp: 2025-11-17T16:47:05.578Z
Learning: In the frontend test files for the OWASP/Nest repository, `expect(true).toBe(true)` no-op assertions may be intentionally added as workarounds when ESLint rule jest/expect-expect doesn't detect expectations inside helper functions or waitFor callbacks. These can be resolved by configuring the ESLint rule's assertFunctionNames option to recognize custom assertion helpers instead of flagging them as redundant.
📚 Learning: 2025-11-17T17:30:42.139Z
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:169-171
Timestamp: 2025-11-17T17:30:42.139Z
Learning: In the OWASP/Nest frontend tests (PR #2671 context), wrapper functions like `expectChaptersCountEqualsThree` that simply call another helper with a fixed parameter (e.g., `expectChaptersCountEquals(3)`) are intentionally used to avoid arrow function callbacks in `waitFor` calls. This pattern prevents adding nesting depth that would trigger rule typescript:S2004. Example: `await waitFor(expectChaptersCountEqualsThree)` avoids the extra nesting from `await waitFor(() => expectChaptersCountEquals(3))`.

Applied to files:

  • frontend/__tests__/unit/components/ChapterMap.test.tsx
🔇 Additional comments (2)
frontend/__tests__/unit/components/ChapterMap.test.tsx (2)

144-144: LGTM: Numeric literal normalized to satisfy Sonar rule.

The change from 1.0 to 1 is stylistically cleaner and satisfies static analysis requirements without affecting behavior.


290-290: LGTM: Non-null assertions correctly removed.

The removal of ! operators on lines 290 and 299 aligns with the PR's objective to eliminate redundant non-null assertions and satisfy Sonar rule TypeScript:S4325.

Also applies to: 299-299


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

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

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 6, 2026
@anukalp2804
Copy link
Contributor Author

All checks pass locally. Please review when convenient. Thank you!

Copy link
Collaborator

@arkid15r arkid15r left a comment

Choose a reason for hiding this comment

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

Please fix all cases as requested in the issue.

@anukalp2804
Copy link
Contributor Author

Please fix all cases as requested in the issue.

Updated the PR. Removed remaining redundant non-null assertions as requested.
All local checks pass.

Copy link
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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
frontend/__tests__/unit/components/ChapterMap.test.tsx (1)

286-302: Incomplete fix: Lines 308 and 317 still use redundant non-null assertions.

The removal of ! from fireEvent.click(overlay) at lines 290 and 299 aligns with the PR objective. However, lines 308 and 317 still contain fireEvent.keyDown(overlay!, ...) with the redundant ! assertion. Since both fireEvent.click() and fireEvent.keyDown() have identical type requirements for their element parameter, these should also be updated for consistency and to fully resolve the Sonar rule violation.

Proposed fix
-      fireEvent.keyDown(overlay!, { key: 'Enter' })
+      fireEvent.keyDown(overlay, { key: 'Enter' })
-      fireEvent.keyDown(overlay!, { key: ' ' })
+      fireEvent.keyDown(overlay, { key: ' ' })
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d537a58 and 107460b.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/ChapterMap.test.tsx
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: rudransh-shrivastava
Repo: OWASP/Nest PR: 2178
File: frontend/src/app/snapshots/[id]/page.tsx:0-0
Timestamp: 2025-09-21T17:04:48.154Z
Learning: User rudransh-shrivastava confirmed that suggested type safety improvements during Apollo Client migration were no longer relevant, reinforcing their preference to keep migration PRs focused on core migration changes rather than additional improvements.
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:427-427
Timestamp: 2025-11-17T16:47:05.578Z
Learning: In the frontend test files for the OWASP/Nest repository, `expect(true).toBe(true)` no-op assertions may be intentionally added as workarounds when ESLint rule jest/expect-expect doesn't detect expectations inside helper functions or waitFor callbacks. These can be resolved by configuring the ESLint rule's assertFunctionNames option to recognize custom assertion helpers instead of flagging them as redundant.
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:169-171
Timestamp: 2025-11-17T17:30:42.139Z
Learning: In the OWASP/Nest frontend tests (PR #2671 context), wrapper functions like `expectChaptersCountEqualsThree` that simply call another helper with a fixed parameter (e.g., `expectChaptersCountEquals(3)`) are intentionally used to avoid arrow function callbacks in `waitFor` calls. This pattern prevents adding nesting depth that would trigger rule typescript:S2004. Example: `await waitFor(expectChaptersCountEqualsThree)` avoids the extra nesting from `await waitFor(() => expectChaptersCountEquals(3))`.
📚 Learning: 2025-11-17T17:30:42.139Z
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:169-171
Timestamp: 2025-11-17T17:30:42.139Z
Learning: In the OWASP/Nest frontend tests (PR #2671 context), wrapper functions like `expectChaptersCountEqualsThree` that simply call another helper with a fixed parameter (e.g., `expectChaptersCountEquals(3)`) are intentionally used to avoid arrow function callbacks in `waitFor` calls. This pattern prevents adding nesting depth that would trigger rule typescript:S2004. Example: `await waitFor(expectChaptersCountEqualsThree)` avoids the extra nesting from `await waitFor(() => expectChaptersCountEquals(3))`.

Applied to files:

  • frontend/__tests__/unit/components/ChapterMap.test.tsx
🔇 Additional comments (1)
frontend/__tests__/unit/components/ChapterMap.test.tsx (1)

290-290: Changes look good, but fix is incomplete.

The removal of non-null assertions from these fireEvent.click calls is correct and improves code clarity. The tests will still fail appropriately if overlay is null, making the assertions unnecessary.

Note: See previous comment about lines 308 and 317 that still need the same fix.

Also applies to: 299-299

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 6, 2026
Copy link
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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
frontend/__tests__/unit/components/ChapterMap.test.tsx (1)

308-308: Complete the non-null assertion removal for consistency.

Lines 308 and 317 still contain non-null assertions (overlay!) in fireEvent.keyDown calls, while lines 290 and 299 had them removed from fireEvent.click calls. Since all four use the same overlay variable from closest('button'), the assertions should be removed consistently.

🔎 Suggested fix to complete the refactor
     const overlay = getByText('Unlock map').closest('button')
-    fireEvent.keyDown(overlay!, { key: 'Enter' })
+    fireEvent.keyDown(overlay, { key: 'Enter' })

     expect(mockMap.scrollWheelZoom.enable).toHaveBeenCalled()

And similarly for line 317:

     const overlay = getByText('Unlock map').closest('button')
-    fireEvent.keyDown(overlay!, { key: ' ' })
+    fireEvent.keyDown(overlay, { key: ' ' })

     expect(mockMap.scrollWheelZoom.enable).toHaveBeenCalled()

Also applies to: 317-317

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 107460b and 497a985.

⛔ Files ignored due to path filters (1)
  • frontend/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/ChapterMap.test.tsx
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: rudransh-shrivastava
Repo: OWASP/Nest PR: 2178
File: frontend/src/app/snapshots/[id]/page.tsx:0-0
Timestamp: 2025-09-21T17:04:48.154Z
Learning: User rudransh-shrivastava confirmed that suggested type safety improvements during Apollo Client migration were no longer relevant, reinforcing their preference to keep migration PRs focused on core migration changes rather than additional improvements.
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:427-427
Timestamp: 2025-11-17T16:47:05.578Z
Learning: In the frontend test files for the OWASP/Nest repository, `expect(true).toBe(true)` no-op assertions may be intentionally added as workarounds when ESLint rule jest/expect-expect doesn't detect expectations inside helper functions or waitFor callbacks. These can be resolved by configuring the ESLint rule's assertFunctionNames option to recognize custom assertion helpers instead of flagging them as redundant.
📚 Learning: 2025-11-17T17:30:42.139Z
Learnt from: anurag2787
Repo: OWASP/Nest PR: 2671
File: frontend/__tests__/unit/components/MultiSearch.test.tsx:169-171
Timestamp: 2025-11-17T17:30:42.139Z
Learning: In the OWASP/Nest frontend tests (PR #2671 context), wrapper functions like `expectChaptersCountEqualsThree` that simply call another helper with a fixed parameter (e.g., `expectChaptersCountEquals(3)`) are intentionally used to avoid arrow function callbacks in `waitFor` calls. This pattern prevents adding nesting depth that would trigger rule typescript:S2004. Example: `await waitFor(expectChaptersCountEqualsThree)` avoids the extra nesting from `await waitFor(() => expectChaptersCountEquals(3))`.

Applied to files:

  • frontend/__tests__/unit/components/ChapterMap.test.tsx
🔇 Additional comments (2)
frontend/__tests__/unit/components/ChapterMap.test.tsx (2)

144-144: LGTM: Numeric literal normalized to satisfy Sonar rule.

The change from 1.0 to 1 is stylistically cleaner and satisfies static analysis requirements without affecting behavior.


290-290: LGTM: Non-null assertions correctly removed.

The removal of ! operators on lines 290 and 299 aligns with the PR's objective to eliminate redundant non-null assertions and satisfy Sonar rule TypeScript:S4325.

Also applies to: 299-299

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 7, 2026
@arkid15r arkid15r enabled auto-merge January 7, 2026 00:36
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 7, 2026

Copy link
Collaborator

@arkid15r arkid15r left a comment

Choose a reason for hiding this comment

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

LGTM

@arkid15r arkid15r added this pull request to the merge queue Jan 7, 2026
@anukalp2804
Copy link
Contributor Author

LGTM

Thanks a lot for the review, guidance, and approval.
Glad it’s in the merge queue — I’d be happy to continue contributing!

Merged via the queue into OWASP:main with commit 45521fb Jan 7, 2026
27 checks passed
hussainjamal760 pushed a commit to hussainjamal760/Nest that referenced this pull request Jan 14, 2026
* fix: remove redundant non-null assertions

* fix: remove remaining redundant non-null assertions

* chore: replace 1.0 with 1 to satisfy sonar rule

* chore: remove unintended package-lock.json from PR

---------

Co-authored-by: Arkadii Yakovets <2201626+arkid15r@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove redundant non-null assertion (!activeItem) in test — Sonar Rule TypeScript:S4325

2 participants