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

Fixes #2669 : Refactored src/screen/PageNotFound component test from jest to vitest #2670

Open
wants to merge 3 commits into
base: develop-postgres
Choose a base branch
from

Conversation

rafidoth
Copy link

@rafidoth rafidoth commented Dec 16, 2024

What kind of change does this PR introduce?
Refactoring
This PR also fixes last #2669 PR which shows 2 file change but only 1 file changed (src/screen/PageNotFound.test.tsx)

Issue Number:
#2568
Fixes #

Snapshots/Videos:
PageNotFound_testpassed

Summary

  • Just refactored from Jest to vitest
  • renamed file and added one import line for vitest

Does this PR introduce a breaking change?
No

Other information

Have you read the contributing guide?
Yes

Summary by CodeRabbit

  • Tests
    • Updated the testing framework for the PageNotFound component to use vitest, enhancing the structure of test cases while maintaining existing functionality.
    • Introduced a new mock file for styles to improve testing scenarios.
    • Enhanced Jest configuration for better handling of CSS modules and stylesheets during testing.

Copy link

coderabbitai bot commented Dec 16, 2024

Walkthrough

The pull request involves migrating the test suite for the PageNotFound component from Jest to Vitest. The changes focus on updating the testing framework imports and restructuring the test cases using Vitest's describe and test functions. Additionally, a new mock file for styles is introduced, and the Jest configuration is updated to accommodate CSS module mocking. The core functionality and assertions of the tests remain unchanged, ensuring that the PageNotFound component continues to render correctly for both regular users and administrators.

Changes

File Change Summary
src/screens/PageNotFound/PageNotFound.spec.tsx Migrated from Jest to Vitest testing framework, replacing imports and restructuring test cases.
__mocks__/styleMock.ts Added a new mock file exporting an empty object for style imports.
jest.config.js Updated module name mapping for CSS modules and stylesheets, and changed setup files from react-app-polyfill/jsdom to whatwg-fetch.

Possibly related issues

Possibly related PRs

Suggested labels

refactor, ignore-sensitive-files-pr

Suggested reviewers

  • palisadoes

Poem

🐰 A testing tale of Vitest's might,
Where PageNotFound now shines so bright!
From Jest to Vitest, code takes flight,
Refactoring tests with pure delight,
A rabbit's cheer for this clean rewrite! 🧪


📜 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 8e66450 and 98f7136.

📒 Files selected for processing (1)
  • jest.config.js (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • jest.config.js

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

@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/screens/PageNotFound/PageNotFound.spec.tsx (1)

Migration requires cleanup of Jest dependencies and configuration

The codebase is in a transitional state with both Jest and Vitest configurations:

  • Vitest is properly set up with vitest.config.ts and dependencies (vitest, @vitest/coverage-istanbul)
  • However, Jest artifacts remain (jest.config.js and 8 Jest-related dependencies)

The test file itself uses Vitest imports (import { test, expect, describe } from 'vitest'), but the presence of Jest configuration and dependencies suggests an incomplete migration. These should be removed to prevent confusion and potential conflicts.

Cleanup needed:

  • Remove jest.config.js
  • Remove Jest-related dependencies from package.json
  • Ensure all test utilities are imported from Vitest instead of Jest
🔗 Analysis chain

Line range hint 1-57: Verify complete migration from Jest to Vitest

Let's ensure no Jest-specific code or patterns remain in the codebase.


Migration to Vitest is incomplete - Jest dependencies and configuration still present

  • Found jest.config.js configuration file still present in the repository
  • Multiple Jest-related dependencies in package.json:
    • Core Jest packages: jest, babel-jest
    • Jest DOM testing: @testing-library/jest-dom
    • Type definitions: @types/jest
    • ESLint plugin: eslint-plugin-jest
    • Mocking utilities: jest-localstorage-mock, jest-location-mock
    • Testing tools: jest-preview

Let's verify if there are any Jest-specific test patterns being used across test files:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining Jest-specific patterns or configurations
echo "Checking for Jest patterns in the test file..."
rg -i "jest\." src/screens/PageNotFound/PageNotFound.spec.tsx

echo "Checking for Jest configuration files..."
fd -g "jest.config.*"

echo "Checking for Jest dependencies in package.json..."
if [ -f "package.json" ]; then
    jq '.dependencies + .devDependencies | with_entries(select(.key | contains("jest")))' package.json
fi

Length of output: 789


Script:

#!/bin/bash
# Search for common Jest-specific patterns in test files
echo "Checking for Jest-specific test patterns..."
rg -i "beforeAll|afterAll|beforeEach|afterEach|jest\.|describe|test|it|expect" --type ts --type tsx -g "*spec*" -g "*test*" -l

# Check if Vitest is properly configured
echo "Checking for Vitest configuration..."
fd -g "vitest.config.*"

# Check if there are any Vitest dependencies
if [ -f "package.json" ]; then
    echo "Checking Vitest dependencies..."
    jq '.dependencies + .devDependencies | with_entries(select(.key | contains("vitest")))' package.json
fi

Length of output: 647

🧹 Nitpick comments (2)
src/screens/PageNotFound/PageNotFound.spec.tsx (2)

Line range hint 16-16: Remove commented code

The commented line //setItem('AdminFor', undefined); appears to be unused. If it's not needed, it should be removed to maintain clean test code.

-  //setItem('AdminFor', undefined);

Line range hint 35-57: Consider adding test cleanup for admin test case

The admin test sets items in localStorage but doesn't clean them up afterward. This could potentially affect other tests.

Consider adding cleanup after the test:

  test('Component should be rendered properly for ADMIN or SUPERADMIN', () => {
    setItem('AdminFor', [
      {
        _id: '6537904485008f171cf29924',
        __typename: 'Organization',
      },
    ]);
+   afterEach(() => {
+     setItem('AdminFor', undefined);
+   });
    render(
📜 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 cbb662d.

📒 Files selected for processing (1)
  • src/screens/PageNotFound/PageNotFound.spec.tsx (1 hunks)
🔇 Additional comments (1)
src/screens/PageNotFound/PageNotFound.spec.tsx (1)

11-11: LGTM: Clean Vitest integration

The Vitest import is correctly added while maintaining the necessary @testing-library/react imports for component rendering.

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 16, 2024
@rafidoth
Copy link
Author

Test Application fails at Jest Test in Chat.test.tsx. This PR changes only src/screen/PageNotFound.test.tsx (src/screen/PageNotFound.spec.tsx)

@palisadoes
Copy link
Contributor

  1. Please fix the failing tests.
    1. You may have to fix a test that is not related to this PR as in some rare cases we merge faulty test code. If this is the case, fix the test in this PR. Do not open another issue.
  2. Please make sure that CodeRabbit.ai approves the PR.
  3. Please fix any conflicting files

Copy link

@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)
jest.config.js (1)

Keep jsdom polyfill in Jest configuration

The removal of react-app-polyfill/jsdom from setupFiles is risky as:

  • The project uses jsdom as test environment (confirmed in both Jest and Vitest configs)
  • There are 140+ Jest test files that haven't been migrated to Vitest yet
  • No direct jsdom polyfill imports were found, suggesting tests rely on the global setup

The safe approach is to keep both polyfills:

- setupFiles: ['whatwg-fetch'],
+ setupFiles: ['react-app-polyfill/jsdom', 'whatwg-fetch'],
🔗 Analysis chain

Line range hint 12-13: Verify the removal of jsdom polyfill

The change from react-app-polyfill/jsdom to whatwg-fetch seems unrelated to the Vitest migration of the PageNotFound component. This modification could potentially break other Jest tests that depend on jsdom polyfills.

Let's verify the impact on other Jest tests:

Consider keeping both polyfills to maintain compatibility:

- // setupFiles: ['react-app-polyfill/jsdom'],
- setupFiles: ['whatwg-fetch'],
+ setupFiles: ['react-app-polyfill/jsdom', 'whatwg-fetch'],
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for jsdom usage in test files
# Expected: No test files should directly rely on jsdom features without the polyfill

# Search for DOM manipulation in test files
rg -l 'document\.|window\.|navigator\.' --type ts --type tsx '.*\.test\.(ts|tsx)$' || echo "No direct DOM usage found"

# List all test files still using Jest (excluding PageNotFound)
fd -e test.tsx -e test.ts | grep -v "PageNotFound.test.tsx" || echo "No other Jest test files found"

Length of output: 10676


Script:

#!/bin/bash
# Let's check if any test files are using DOM-related features that might need jsdom
rg -l -t ts -t tsx 'render\(|screen\.|fireEvent\.|userEvent\.' '.*\.test\.(ts|tsx)$' || echo "No testing-library DOM usage found"

# Check if there are any direct imports of jsdom-related polyfills
rg -l "import.*react-app-polyfill/jsdom" || echo "No direct jsdom polyfill imports found"

# Check setupFiles configuration in other test setup files
fd "setup|config" -e js -e ts | xargs cat 2>/dev/null | grep -i "setupFiles\|jsdom\|whatwg" || echo "No other setup files found"

Length of output: 628

🧰 Tools
🪛 eslint

[error] 46-46: Delete ·

(prettier/prettier)

🧹 Nitpick comments (1)
jest.config.js (1)

Line range hint 1-85: Architecture Advice: Consider Separating Test Configurations

Since you're gradually migrating tests from Jest to Vitest, consider maintaining separate configurations to avoid confusion and potential conflicts:

  1. Keep Jest config focused on remaining Jest tests
  2. Use a separate Vitest config for migrated tests
  3. Document the migration strategy

Would you like help creating a migration strategy document or setting up a separate Vitest configuration?

🧰 Tools
🪛 eslint

[error] 46-46: Delete ·

(prettier/prettier)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cbb662d and 8e66450.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • __mocks__/styleMock.ts (1 hunks)
  • jest.config.js (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • mocks/styleMock.ts
🧰 Additional context used
🪛 eslint
jest.config.js

[error] 46-46: Delete ·

(prettier/prettier)

🔇 Additional comments (1)
jest.config.js (1)

45-46: 🛠️ Refactor suggestion

Question: Are these CSS module changes necessary?

The PR's objective is to migrate a single test file to Vitest, but these CSS module mappings affect all Jest tests. While these changes might fix CSS-related test failures:

  1. They seem beyond the scope of the PR
  2. They might mask underlying issues that should be addressed separately

Additionally, there's a trailing whitespace on line 46 that should be removed.

Let's verify if these mappings are actually needed:

If these changes are necessary:

  1. They should be part of a separate PR focused on fixing CSS module handling
  2. The trailing whitespace should be removed:
-    '\\.(css|less|scss|sass)$': '<rootDir>/__mocks__/styleMock.ts', 
+    '\\.(css|less|scss|sass)$': '<rootDir>/__mocks__/styleMock.ts',
🧰 Tools
🪛 eslint

[error] 46-46: Delete ·

(prettier/prettier)

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 16, 2024
@palisadoes
Copy link
Contributor

  1. Please fix the failing tests. Click on the Details links for more information.
  2. Please make sure that CodeRabbit.ai approves your changes

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.

2 participants