Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: src/screens/OrganizationDashboard from Jest to Vitest #2678

Conversation

abbi4code
Copy link
Contributor

@abbi4code abbi4code commented Dec 18, 2024

What kind of change does this PR introduce?
This PR migrates the OrganizationDashboard tests from Jest to Vitest and addresses all failing tests to ensure they pass successfully

Issue Number:

Fixes #2558

Did you add tests for your changes?
Yes

Snapshots/Videos:
Screenshot from 2024-12-18 14-54-12

Reason for Adding identity-obj-proxy for CSS Modules:

The purpose of adding the identity-obj-proxy mapping and the CSS transformation is to properly parse CSS Modules in Jest.However, CSS Modules by default may still get ignored by Jest's configuration. To ensure proper handling, we need to map CSS Modules to identity-obj-proxy, which allows Jest to mock and return class names as string values. This avoids errors in Jest tests and ensures that styles are correctly interpreted in the test environment.

Alternative Approach with jest-css-modules-transform
I also got a alternative approach in which i can replace the below configuration with jest-css-modules-transform, which specifically targets CSS Modules.

  transform: {
    '^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css', // CSS transformations'
  },
  moduleNameMapper: {
    '\\.(css|less)$': 'identity-obj-proxy',
  },

with

transform: {
  '^.+\\.(css|scss|sass|less)$': 'jest-css-modules-transform',  
  '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': 'jest-preview/transforms/file', 
}

Replacing jest-preview/transforms/css with jest-css-modules-transform:
The key difference with this approach is that jest-css-modules-transform is specifically designed for transforming , parsing and mocking CSS Modules in a Jest environment. It automatically handles CSS Modules by transforming them into mockable objects, which eliminates the need for using identity-obj-proxy.

Test Cases Results(Using 1st approach)

Screenshot from 2024-12-18 14-50-24

Summary by CodeRabbit

  • Tests
    • Enhanced the test suite for the OrganizationScreen component with improved assertions using module-scoped styles.
    • Updated the OrganizationDashboard component tests to use Vitest for mocking, with detailed comments and structured test cases for various scenarios.

Copy link
Contributor

coderabbitai bot commented Dec 18, 2024

Walkthrough

The pull request involves two test files: OrganizationScreen.test.tsx and OrganizationDashboard.spec.tsx. The changes primarily focus on updating the testing approach, with the OrganizationScreen test modifying class name assertions to use module-scoped styles instead of hardcoded values. The OrganizationDashboard test transitions from Jest to Vitest mocking techniques, enhancing the flexibility and structure of the tests. These modifications align with the project's ongoing migration to the Vitest testing framework.

Changes

File Change Summary
src/components/OrganizationScreen/OrganizationScreen.test.tsx Updated class name assertions to use styles module references instead of hardcoded values
src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx Migrated from Jest to Vitest mocking, updated react-toastify and react-router-dom mocks

Assessment against linked issues

Objective Addressed Explanation
Replace Jest-specific functions with Vitest equivalents [#2558]
Ensure tests pass using npm run test:vitest [#2558] Requires full test suite verification.
Maintain 100% test coverage [#2558] Requires coverage report confirmation.

Possibly related issues

Suggested reviewers

  • varshith257

Poem

🐰 Hopping through code with glee,
Jest and Vitest dance with me,
Tests refactored, assertions bright,
Our dashboard shines with testing might!
Rabbit's code review is pure delight! 🧪


📜 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 5b77244 and e6465f6.

📒 Files selected for processing (1)
  • src/components/OrganizationScreen/OrganizationScreen.test.tsx (0 hunks)
💤 Files with no reviewable changes (1)
  • src/components/OrganizationScreen/OrganizationScreen.test.tsx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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. (Beta)
  • @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

Our Pull Request Approval Process

Thanks for contributing!

Testing Your Code

Remember, your PRs won't be reviewed until these criteria are met:

  1. We don't merge PRs with poor code quality.
    1. Follow coding best practices such that CodeRabbit.ai approves your PR.
  2. We don't merge PRs with failed tests.
    1. When tests fail, click on the Details link to learn more.
    2. Write sufficient tests for your changes (CodeCov Patch Test). Your testing level must be better than the target threshold of the repository
    3. Tests may fail if you edit sensitive files. Ask to add the ignore-sensitive-files-pr label if the edits are necessary.
  3. We cannot merge PRs with conflicting files. These must be fixed.

Our policies make our code better.

Reviewers

Do not assign reviewers. Our Queue Monitors will review your PR and assign them.
When your PR has been assigned reviewers contact them to get your code reviewed and approved via:

  1. comments in this PR or
  2. our slack channel

Reviewing Your Code

Your reviewer(s) will have the following roles:

  1. arbitrators of future discussions with other contributors about the validity of your changes
  2. point of contact for evaluating the validity of your work
  3. person who verifies matching issues by others that should be closed.
  4. person who gives general guidance in fixing your tests

CONTRIBUTING.md

Read our CONTRIBUTING.md file. Most importantly:

  1. PRs with issues not assigned to you will be closed by the reviewer
  2. Fix the first comment in the PR so that each issue listed automatically closes

Other

  1. 🎯 Please be considerate of our volunteers' time. Contacting the person who assigned the reviewers is not advised unless they ask for your input. Do not @ the person who did the assignment otherwise.
  2. Read the CONTRIBUTING.md file make

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

🔭 Outside diff range comments (1)
src/components/OrganizationScreen/OrganizationScreen.test.tsx (1)

Line range hint 16-17: Migrate Jest mocks to Vitest

As part of the Jest to Vitest migration, update the mocking syntax:

-jest.mock('react-router-dom', () => ({
+vi.mock('react-router-dom', () => ({
   ...jest.requireActual('react-router-dom'),
   useParams: () => ({ orgId: mockID }),
   useMatch: () => ({ params: { eventId: 'event123', orgId: '123' } }),
}));
🧹 Nitpick comments (3)
jest.config.js (1)

Line range hint 1-89: Consider removing jest.config.js if migrating to Vitest

Since the PR objective is to migrate tests from Jest to Vitest, maintaining both Jest and Vitest configurations might lead to confusion. Consider:

  1. Moving all tests to Vitest
  2. Removing jest.config.js
  3. Ensuring all configurations are in vitest.config.ts
src/components/OrganizationScreen/OrganizationScreen.test.tsx (1)

79-85: Remove debug console.log statements

Debug console.log statements should be removed before committing:

-    console.log('before close', screen.getByTestId('mainpageright').className);
-    console.log('after close', screen.getByTestId('mainpageright').className);
-    console.log('styles', styles.expand);
-    console.log('before 800', screen.getByTestId('mainpageright').className);
-    console.log('******', screen.getByTestId('mainpageright').className);
-    console.log('styles expanddd', styles.expand);

Also applies to: 96-100

src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx (1)

Line range hint 1-311: Consider consistent test file extensions

The project uses both .test.tsx and .spec.tsx extensions for test files. Consider standardizing on one extension throughout the project for consistency:

  • OrganizationScreen.test.tsx
  • OrganizationDashboard.spec.tsx

Choose either .test.tsx or .spec.tsx and apply it consistently.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9723078 and a7796e1.

📒 Files selected for processing (3)
  • jest.config.js (2 hunks)
  • src/components/OrganizationScreen/OrganizationScreen.test.tsx (1 hunks)
  • src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx (13 hunks)
🧰 Additional context used
📓 Learnings (1)
jest.config.js (1)
Learnt from: MayankJha014
PR: PalisadoesFoundation/talawa-admin#2619
File: vitest.config.ts:0-0
Timestamp: 2024-12-09T15:54:04.872Z
Learning: The files `scripts/custom-test-env.js`, `src/utils/i18nForTest.ts`, and `vitest.setup.ts` are not test files and should not be included in the test pattern.
🔇 Additional comments (3)
jest.config.js (1)

39-39: LGTM: CSS module mapping is correctly configured

The addition of identity-obj-proxy for CSS/LESS files is the correct approach for handling CSS modules in tests.

src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx (2)

20-31: LGTM: Well-documented test suite

The comprehensive documentation clearly outlines the test coverage and scenarios. This is excellent practice for maintaining test suites.


33-38: LGTM: Proper Vitest migration

The migration from Jest to Vitest mocks is done correctly:

  • Using vi.mock() instead of jest.mock()
  • Using vi.fn() instead of jest.fn()
  • Proper async module mocking

Also applies to: 106-112

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 18, 2024
coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 18, 2024
@abbi4code
Copy link
Contributor Author

abbi4code commented Dec 20, 2024

@Cioppolo14 @varshith257 I have fixed all the failing tests . Could you please review it ?

Copy link

codecov bot commented Dec 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.29%. Comparing base (4831b84) to head (e6465f6).
Report is 1 commits behind head on develop-postgres.

Additional details and impacted files
@@                 Coverage Diff                  @@
##           develop-postgres    #2678      +/-   ##
====================================================
+ Coverage             83.81%   86.29%   +2.47%     
====================================================
  Files                   295      312      +17     
  Lines                  7274     8134     +860     
  Branches               1592     1841     +249     
====================================================
+ Hits                   6097     7019     +922     
- Misses                  940      943       +3     
+ Partials                237      172      -65     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

varshith257
varshith257 previously approved these changes Dec 20, 2024
jest.config.js Outdated Show resolved Hide resolved
@varshith257 varshith257 dismissed stale reviews from coderabbitai[bot] and themself via 5b77244 December 20, 2024 01:53
@varshith257
Copy link
Member

It seems the changes doesn't include into the coverage of vitest. Take a Lokok

@abbi4code
Copy link
Contributor Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Dec 21, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants