-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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: Simplified and improved the Jest test #3846
base: canary
Are you sure you want to change the base?
refactor: Simplified and improved the Jest test #3846
Conversation
|
@Ultimecia1463 is attempting to deploy a commit to the NextUI Inc Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe changes involve updates to three script files: Changes
Suggested labels
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (4)
scripts/setup-test.ts (2)
4-8
: Improved mock implementation forwindow.matchMedia
The changes to the
window.matchMedia
mock implementation are good. The conditional check prevents unnecessary overwriting, and theconfigurable: true
setting allows for flexibility in testing.Consider adding a comment explaining why
enumerable
was removed from the property descriptor, as it might not be immediately clear to other developers.
31-33
: ImprovedreactStrictMode
configurationThe update to the
reactStrictMode
configuration is a significant improvement. UsingJSON.parse
ensures correct boolean parsing of theSTRICT_MODE
environment variable, with a default value offalse
if not set.For added clarity, consider adding a comment explaining the expected values for the
STRICT_MODE
environment variable:// STRICT_MODE environment variable should be 'true' or 'false' reactStrictMode: JSON.parse(process.env.STRICT_MODE || "false"),scripts/check-rap-updates.ts (2)
13-19
: Great improvements to concurrency and filteringThe changes here are excellent:
- Concurrent processing of package.json files will improve performance.
- The regex filter correction ensures proper package name matching.
- Using the
shouldUpgrade
flag for theupgrade
option is consistent.Consider adding a comment explaining the regex pattern for better maintainability:
filter: /^@react-(aria|stately|types)\/.*/, // Matches @react-aria/*, @react-stately/*, and @react-types/* packages
40-46
: Excellent main function improvementsThe enhancements to the main function are commendable:
- Concurrent processing of directories for better performance.
- Added error handling to improve script robustness.
- Clear completion message for better user feedback.
These changes significantly improve the script's efficiency and error handling.
Consider adding more detailed error logging:
console.error('Error during package checks:', error.message, '\nStack:', error.stack);This would provide more context for debugging in case of errors.
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (3)
- scripts/check-rap-updates.ts (1 hunks)
- scripts/fix-rap.ts (1 hunks)
- scripts/setup-test.ts (3 hunks)
🔇 Additional comments (13)
scripts/setup-test.ts (2)
22-22
: Improved code readabilityThe addition of an empty line enhances the code's readability by clearly separating different sections of the setup.
Line range hint
1-33
: Overall improvements to test setupThe changes in this file successfully achieve the PR objectives of simplifying and enhancing the Jest test setup. Key improvements include:
- More robust
window.matchMedia
mocking- Better code organization
- Improved handling of the
reactStrictMode
configurationThese changes contribute to a more maintainable and efficient test environment. Great work!
scripts/check-rap-updates.ts (4)
5-7
: LGTM: Upgrade flag check addedThe addition of the
shouldUpgrade
constant is a good practice. It allows for more flexible control over the script's behavior through command-line arguments.
23-30
: Improved logging and output clarityThe enhancements to the logging are valuable:
- Clearer output for upgrades found.
- New condition to log when no updates are found, improving completeness.
- Conditional logging for upgrades, consistent with the
shouldUpgrade
flag.These changes will make it easier to understand the script's actions and results.
33-33
: Enhanced error loggingThe improved error logging, which now includes the file path, is an excellent addition. This will significantly aid in debugging by providing more context when errors occur.
35-35
: Correct Promise.all closureThe closing bracket for the Promise.all call is correctly placed, matching the opening on line 14.
scripts/fix-rap.ts (7)
3-3
: Good Practice: Using 'fs' Promises API for Asynchronous File OperationsThe import statement correctly uses the promises API from the
'fs'
module, facilitating asynchronous file operations without blocking the event loop.
9-9
: Regex Update: Now Includes '@react-types' DependenciesThe regular expression has been updated to include
'@react-types'
, ensuring that dependencies matching'@react-aria'
,'@react-stately'
, and'@react-types'
are processed appropriately.
17-17
: Updated 'processPackageFiles' Function to be AsynchronousThe
processPackageFiles
function is now declared asasync
, allowing for non-blocking I/O operations and improved performance.
22-36
: Refactored File Processing to Use Asynchronous Operations and ConcurrencyThe code within
processPackageFiles
has been refactored to usePromise.all
andasync/await
, processing the package files concurrently. This change enhances performance by allowing multiple file operations to occur simultaneously.
25-26
: Proper Use of Asynchronous File Reading and JSON ParsingThe code correctly uses
await fs.readFile
to read file contents asynchronously and then parses the JSON data, ensuring efficient, non-blocking operation.
30-31
: Proper Use of Asynchronous File WritingThe code correctly uses
await fs.writeFile
to write the updated package data asynchronously and provides feedback upon successful operation.
42-46
: Asynchronous 'main' Function with Concurrent Directory ProcessingThe
main
function has been updated to be asynchronous and now processes directories concurrently usingPromise.all
. This improves the script's performance when dealing with multiple directories.
const filePaths = glob.sync(resolve(path, '**/package.json'), { | ||
ignore: '**/node_modules/**', | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Suggestion: Use Asynchronous 'glob' Function for Consistency
While processPackageFiles
is now asynchronous, it still uses the synchronous glob.sync
method. Consider using the asynchronous glob
function to avoid blocking the event loop and ensure full asynchronicity in your file operations.
You can modify the code as follows:
- const filePaths = glob.sync(resolve(path, '**/package.json'), {
- ignore: '**/node_modules/**',
- });
+ const filePaths = await glob(resolve(path, '**/package.json'), {
+ ignore: '**/node_modules/**',
+ });
If glob
does not natively support promises, you can use util.promisify
to convert it:
import { promisify } from 'util';
const globAsync = promisify(glob);
Then update the code:
const filePaths = await globAsync(resolve(path, '**/package.json'), {
ignore: '**/node_modules/**',
});
This change ensures that all file system operations within processPackageFiles
are fully asynchronous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocked window.matchMedia and global.ResizeObserver using jest.fn with enhanced structure.
Made the test setup more maintainable and efficient without changing core logic.
which part of code refers to these statements?
|
||
if(shouldUpgrade && upgraded) { | ||
console.log(`✅ Upgraded packages in ${filePath}`); | ||
if (Object.keys(upgraded).length > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No overload matches this call.
Overload 1 of 2, '(o: {}): string[]', gave the following error.
Argument of type 'void | PackageFile | Index<string>' is not assignable to parameter of type '{}'.
Type 'void' is not assignable to type '{}'.
Overload 2 of 2, '(o: object): string[]', gave the following error.
Argument of type 'void | PackageFile | Index<string>' is not assignable to parameter of type 'object'.
Type 'void' is not assignable to type 'object'.ts(2769)
@@ -22,7 +19,7 @@ if (typeof window.matchMedia !== "function") { | |||
}); | |||
} | |||
|
|||
// Workaround https://github.com/jsdom/jsdom/issues/2524#issuecomment-897707183 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this line removed? it's not resolved.
configure({ | ||
reactStrictMode: process.env.STRICT_MODE === "true", | ||
reactStrictMode: JSON.parse(process.env.STRICT_MODE || "false"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in which case this change would benefit?
📝 Description
⛳️ Current behavior (updates)
🚀 New behavior
💣 Is this a breaking change (Yes/No):
No
Summary by CodeRabbit
New Features
Bug Fixes
Chores