Skip to content

Comments

Fix Issue: #1076 Date test fixed for Contribute to handle various cases.#1077

Merged
arkid15r merged 5 commits intoOWASP:mainfrom
KaranNegi20Feb:date-test-fix
Mar 12, 2025
Merged

Fix Issue: #1076 Date test fixed for Contribute to handle various cases.#1077
arkid15r merged 5 commits intoOWASP:mainfrom
KaranNegi20Feb:date-test-fix

Conversation

@KaranNegi20Feb
Copy link
Contributor

Solves issue: Take care of flaky e2e tests #1076
As discussed in the issue comments the final solution can be looked here:
#1076 (comment)
#1076 (comment)

Proposed solution handles:

Used regex and it works fine passes everything.
handles for:

"3 months ago"
"a month ago"
"4 months ago"
"a day ago"
"15 days ago"
"30 days ago"

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 11, 2025

Summary by CodeRabbit

  • New Features

    • The Contribute page now displays contribution dates using dynamic relative time (e.g., "4 months ago") for a more accurate reflection of activity.
  • Tests

    • Automated checks have been updated to verify the new relative time display on the Contribute page.

Walkthrough

The pull request introduces the dayjs library with its relativeTime plugin to enhance date handling in the test data. It replaces a hardcoded Unix timestamp for the created_at field with a dynamic calculation representing a date four months ago. Additionally, the objectID field in the issue object is modified, resulting in a single objectID. Correspondingly, an end-to-end test is updated to reflect this change by adjusting the expected text to "4 months ago".

Changes

Files Summary
frontend/tests/unit/data/mockContributeData.ts Added dayjs and relativeTime plugin; updated created_at field to use dynamic timestamp for four months ago; modified objectID field.
frontend/tests/e2e/pages/Contribute.spec.ts Updated expected text in test from "3 months ago" to "4 months ago" for visibility check.

Possibly related PRs

Suggested reviewers

  • kasya

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bca87d7 and be841e8.

📒 Files selected for processing (2)
  • frontend/__tests__/e2e/pages/Contribute.spec.ts (1 hunks)
  • frontend/__tests__/unit/data/mockContributeData.ts (1 hunks)
🔇 Additional comments (4)
frontend/__tests__/e2e/pages/Contribute.spec.ts (1)

20-20: Updated expected text to match new mock data.

The test now checks for "4 months ago" instead of "3 months ago", which aligns with the changes made to the mock data timestamp in mockContributeData.ts. This change helps make the tests more stable by using consistent relative time formatting.

frontend/__tests__/unit/data/mockContributeData.ts (3)

1-4: Great addition of the dayjs library for handling relative time.

Adding dayjs with the relativeTime plugin provides a more robust way to handle date formatting in tests. This is a good approach for making the tests less brittle when dealing with time-based data.


10-10: Excellent dynamic date calculation approach.

Using dayjs().subtract(4, 'months').unix() instead of a hardcoded timestamp ensures that the test data will always be consistently "4 months ago" relative to when the test runs. This addresses the core issue of flaky date-related tests by providing a consistent relative time format.


13-13: Object ID formatting updated.

The objectID has been properly formatted. This change appears to be a minor cleanup or consolidation of the field format.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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

🧹 Nitpick comments (1)
frontend/__tests__/e2e/pages/Contribute.spec.ts (1)

20-22: Great improvement to test robustness!

The regex pattern now handles various time formats including "X months ago", "a month ago", "a day ago", and "X days ago", which makes the test more resilient against timing variations.

Consider adding patterns for:

  • "hours ago" or "minutes ago" for very recent contributions
  • "years ago" for older contributions

This would make the test even more robust for edge cases.

-      page.locator('text=/\\d+ months? ago|a month ago|a day ago|\\d+ days? ago/')
+      page.locator('text=/\\d+ months? ago|a month ago|a day ago|\\d+ days? ago|\\d+ hours? ago|\\d+ minutes? ago|\\d+ years? ago|a year ago/')
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2ab00ee and 0f6771c.

📒 Files selected for processing (1)
  • frontend/__tests__/e2e/pages/Contribute.spec.ts (1 hunks)

@KaranNegi20Feb KaranNegi20Feb changed the title Issue solved: ##1076 Date test fixed for Contribute to handle various cases. Issue solved: #1076 Date test fixed for Contribute to handle various cases. Mar 11, 2025
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.

Yeah, this probably works. Consider this:

test('renders issue data correctly', async ({ page }) => {
await expect(page.getByRole('link', { name: 'Contribution 1' })).toBeVisible()
await expect(page.getByText('3 months ago')).toBeVisible()
await expect(
Copy link
Collaborator

@arkid15r arkid15r Mar 11, 2025

Choose a reason for hiding this comment

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

This should cover all different cases: day, week, month, year -- singular and plural.
Maybe instead mock the data to always have the issue last updated date as 3 month ago?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright I'll do it for day week month year👍, well as far as I have seen the data it always comes in such manner 1 day ago 15 days ago 1 month ago maybe mocking the data is the play. That's a good suggestion in respective of days if days go over the 7 (day>7 will convert it to week ). I suppose that's fine @arkid15r

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you have 2 implementations that solve a problem always prefer the most simple one. I recommend going with the test data adjustment if that works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah well that changing tests sounds ok to me, plus it's a small job, happy me😂

@KaranNegi20Feb
Copy link
Contributor Author

Hey @arkid15r i have fixed the required test cases for dates. Thank you.

@KaranNegi20Feb KaranNegi20Feb requested a review from arkid15r March 11, 2025 21:21
@KaranNegi20Feb KaranNegi20Feb changed the title Issue solved: #1076 Date test fixed for Contribute to handle various cases. Fix Issue : #1076 Date test fixed for Contribute to handle various cases. Mar 11, 2025
@KaranNegi20Feb KaranNegi20Feb changed the title Fix Issue : #1076 Date test fixed for Contribute to handle various cases. Fix Issue: #1076 Date test fixed for Contribute to handle various cases. Mar 11, 2025
test('renders issue data correctly', async ({ page }) => {
await expect(page.getByRole('link', { name: 'Contribution 1' })).toBeVisible()
await expect(page.getByText('3 months ago')).toBeVisible()
await expect(page.locator('text=/\\b(\\d+|a) (day|week|month|year)s? ago\\b/')).toBeVisible()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not adjust the date to be always 3 months old for this test instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What exactly do we mean by that, do I need to change the mock data for testing?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, for this test case only #1077 (comment)

Copy link
Contributor Author

@KaranNegi20Feb KaranNegi20Feb Mar 11, 2025

Choose a reason for hiding this comment

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

So do we both regex code and the mock data changed or just the mock data changes??

Copy link
Collaborator

Choose a reason for hiding this comment

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

One solution is enough and from my perspective the mock data related one is cheaper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll make a PR in some time.

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

🧹 Nitpick comments (2)
frontend/__tests__/unit/data/mockContributeData.ts (2)

10-10: Consider updating the updated_at timestamp too.

While you've updated the created_at field to use a dynamic timestamp, the updated_at field on line 18 still uses a hardcoded value (1734727031). This might lead to inconsistencies in tests if they rely on both timestamps being relative to each other or to the current date.

      created_at: threeMonthsAgo.unix(),
      hint: 'Hint',
      labels: [],
      project_name: 'Owasp Nest',
      project_url: 'https://owasp.org/www-project-nest',
      repository_languages: ['Python', 'TypeScript'],
      summary: 'This is a summary of Contribution 1',
      title: 'Contribution 1',
-     updated_at: 1734727031,
+     updated_at: dayjs().subtract(2, 'months').unix(),

1-4: Consider adding a brief comment explaining the date handling approach.

Adding a brief comment explaining the purpose of using dayjs for calculating relative timestamps would help other developers understand the reasoning behind this implementation, especially since it's addressing a specific issue with flaky tests.

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime.js'
dayjs.extend(relativeTime)
+// Using dynamic timestamps to prevent flaky tests caused by hardcoded dates
+// This ensures tests will pass regardless of when they're run
const threeMonthsAgo = dayjs().subtract(3, 'months')
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 76119ae and bca87d7.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/data/mockContributeData.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Run frontend unit tests
  • GitHub Check: CodeQL (python)
  • GitHub Check: Run frontend e2e tests
  • GitHub Check: CodeQL (javascript-typescript)
  • GitHub Check: Run backend tests
🔇 Additional comments (1)
frontend/__tests__/unit/data/mockContributeData.ts (1)

1-4: Good implementation of dynamic date calculation.

Nice work implementing the dayjs library to create a dynamic "3 months ago" timestamp. This change addresses the issue of flaky tests by ensuring the test data's date always represents a relative point in time rather than a fixed timestamp.

@KaranNegi20Feb KaranNegi20Feb requested a review from arkid15r March 12, 2025 10:18
@KaranNegi20Feb
Copy link
Contributor Author

Hey @arkid15r i have made sure that it always sends the date as 3 months in the relative format.
Thank you for guiding through.

@arkid15r arkid15r linked an issue Mar 12, 2025 that may be closed by this pull request
2 tasks
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 enabled auto-merge March 12, 2025 16:08
@sonarqubecloud
Copy link

@arkid15r arkid15r added this pull request to the merge queue Mar 12, 2025
Merged via the queue into OWASP:main with commit a186724 Mar 12, 2025
18 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Apr 10, 2025
1 task
shdwcodr pushed a commit to shdwcodr/Nest that referenced this pull request Jun 5, 2025
…s cases. (OWASP#1077)

* date test fixed

* more cases added

* date mock data fixed to always be 3 months

* Update code

---------

Co-authored-by: Arkadii Yakovets <arkadii.yakovets@owasp.org>
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.

Take care of flaky e2e tests

2 participants