test: add comprehensive test suite for milady-root utility#274
test: add comprehensive test suite for milady-root utility#274Dexploarer wants to merge 1 commit intodevelopfrom
Conversation
Creates a new Vitest test suite `src/utils/milady-root.test.ts` to provide 100% coverage for `resolveMiladyPackageRoot` and `resolveMiladyPackageRootSync`. Tests correctly mock file system interactions using `vi.mock` on `node:fs` and `node:fs/promises`, checking both successful path resolution (via argv1, cwd, and moduleUrl) and failure modes like missing files and unmatching package.json names.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
| vi.mocked(fs.readFile).mockImplementation(async (filepath) => { | ||
| if ( | ||
| filepath | ||
| .toString() | ||
| .includes(path.join("/project/node_modules/.bin", "package.json")) | ||
| ) { | ||
| throw new Error("ENOENT"); | ||
| } | ||
| if ( | ||
| filepath | ||
| .toString() | ||
| .includes(path.join("/project/node_modules/milady", "package.json")) | ||
| ) { | ||
| return JSON.stringify({ name: "milady" }); | ||
| } | ||
| throw new Error("ENOENT"); | ||
| }); |
There was a problem hiding this comment.
The error simulation in the mock implementation throws a generic Error with the message 'ENOENT'. In real Node.js usage, file system errors such as missing files typically have a code property (e.g., err.code === 'ENOENT'). If the implementation under test checks for err.code, these mocks may not accurately simulate the real error, potentially leading to misleading test results.
Recommendation: Throw an error object with a code property, e.g.:
const err = new Error('ENOENT');
err.code = 'ENOENT';
throw err;This will better simulate Node.js file system errors.
| vi.mock("node:fs/promises", () => ({ | ||
| default: { | ||
| readFile: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("node:fs", () => ({ | ||
| default: { | ||
| readFileSync: vi.fn(), | ||
| }, | ||
| })); |
There was a problem hiding this comment.
The test suite mocks node:fs/promises and node:fs at the top level, but if the implementation of resolveMiladyPackageRoot or resolveMiladyPackageRootSync imports these modules in a different way (e.g., via a different import path or through a dependency), the mocks may not be effective. This could result in tests that do not actually isolate file system access, leading to unreliable or non-deterministic test outcomes.
Recommendation: Ensure that the modules are imported and mocked consistently between the test and the implementation. Consider using Vitest's vi.mock with the actual import path used in the implementation, and verify that the mocks are being applied as intended.
There was a problem hiding this comment.
Code Review
This pull request adds a comprehensive test suite for the milady-root utility, which was previously untested. The tests cover various scenarios for both synchronous and asynchronous root resolution, including edge cases. The overall test structure is good, but there are opportunities to make the mocks for file system access more robust. Specifically, several tests use String.prototype.includes() for path matching, which can be brittle. I've suggested using strict equality checks for paths to improve test reliability.
| vi.mocked(fs.readFile).mockImplementation(async (filepath) => { | ||
| if ( | ||
| filepath | ||
| .toString() | ||
| .includes(path.join("/project/node_modules/.bin", "package.json")) | ||
| ) { | ||
| throw new Error("ENOENT"); | ||
| } | ||
| if ( | ||
| filepath | ||
| .toString() | ||
| .includes(path.join("/project/node_modules/milady", "package.json")) | ||
| ) { | ||
| return JSON.stringify({ name: "milady" }); | ||
| } | ||
| throw new Error("ENOENT"); | ||
| }); |
There was a problem hiding this comment.
The mock implementation for fs.readFile is a bit brittle because it uses includes() for path matching. This can lead to false positives if file paths share substrings. It's more robust to use strict equality (===) with the expected full path. Also, the mock can be simplified. The function under test is expected to handle file-not-found errors, so we only need to mock the successful file read and let all other attempts fail.
vi.mocked(fs.readFile).mockImplementation(async (filepath) => {
if (
filepath.toString() ===
path.join("/project/node_modules/milady", "package.json")
) {
return JSON.stringify({ name: "milady" });
}
throw new Error("ENOENT");
});| vi.mocked(fs.readFile).mockImplementation(async (filepath) => { | ||
| if ( | ||
| filepath.toString().includes(path.join("/test/dir", "package.json")) | ||
| ) { | ||
| return JSON.stringify({ name: "milady" }); | ||
| } | ||
| throw new Error("ENOENT"); | ||
| }); |
There was a problem hiding this comment.
For robustness, it's better to use strict path matching (===) instead of includes(). This avoids potential false positives in tests from partial path matches.
vi.mocked(fs.readFile).mockImplementation(async (filepath) => {
if (filepath.toString() === path.join("/test/dir", "package.json")) {
return JSON.stringify({ name: "milady" });
}
throw new Error("ENOENT");
});| vi.mocked(fs.readFile).mockImplementation(async (filepath) => { | ||
| if ( | ||
| filepath | ||
| .toString() | ||
| .includes(path.join(path.resolve("/module/dir"), "package.json")) | ||
| ) { | ||
| return JSON.stringify({ name: "milady" }); | ||
| } | ||
| throw new Error("ENOENT"); | ||
| }); |
There was a problem hiding this comment.
Using strict path matching (===) instead of includes() makes the mock more robust and prevents potential test brittleness from partial path matches.
vi.mocked(fs.readFile).mockImplementation(async (filepath) => {
if (
filepath.toString() ===
path.join(path.resolve("/module/dir"), "package.json")
) {
return JSON.stringify({ name: "milady" });
}
throw new Error("ENOENT");
});| vi.mocked(fsSync.readFileSync).mockImplementation((filepath) => { | ||
| if ( | ||
| filepath.toString().includes(path.join("/test/dir", "package.json")) | ||
| ) { | ||
| return JSON.stringify({ name: "milady" }); | ||
| } | ||
| throw new Error("ENOENT"); | ||
| }); |
There was a problem hiding this comment.
Similar to other tests, using strict equality for path matching in this mock will make the test more robust and less prone to errors from partial matches.
vi.mocked(fsSync.readFileSync).mockImplementation((filepath) => {
if (filepath.toString() === path.join("/test/dir", "package.json")) {
return JSON.stringify({ name: "milady" });
}
throw new Error("ENOENT");
});
What:
Created a new test file
src/utils/milady-root.test.tsto add test coverage forsrc/utils/milady-root.ts.Why:
The
milady-root.tsutility lacked test coverage. Given its role in resolving critical application paths, it required thorough validation. The new tests verify both synchronous and asynchronous root resolution behaviors against multiple edge cases, such as resolving from binary paths, fallback configurations, and missing files without altering the production logic.Verification:
vitestcovering both success and edge-case failure branches.@biomejs/biome.PR created automatically by Jules for task 10686876895649914840 started by @Dexploarer