test: add coverage for phantom-utils (getDeclaredPackages and installCmd) - #1124
test: add coverage for phantom-utils (getDeclaredPackages and installCmd)#1124emihesi99 wants to merge 1 commit into
Conversation
…Cmd) Adds unit tests for the two pure helper functions in src/overrides/detectors/phantom-utils.ts. Covers: - getDeclaredPackages: empty package.json, all four dependency sections, ignored non-object sections (array/null/string), deduplication, scoped names - installCmd: pnpm/yarn/bun/npm mappings and the npm fallback for unknown package managers Closes OWASP#1088 Signed-off-by: emihesi99 <lmr.alefl@gmail.com>
sonukapoor
left a comment
There was a problem hiding this comment.
Thanks for this, and sorry your comments on the issue went unanswered before you started.
Something you cannot see from your end: I ran your tests against 24 deliberately broken versions of phantom-utils.ts to check they would actually catch a regression. They killed 19 of the 20 that are reachable. That is well above average for a test-only change, and a lot more than "the tests pass".
The part I liked most is the three malformed-section tests. getDeclaredPackages has a three-clause guard and you wrote exactly one test per clause with no redundancy. Drop !Array.isArray and only the array test fails. Drop the typeof check and only the string test fails. Drop the null check and only the null test fails. The issue asked for the array and null cases, so the string case is yours, and it is the only thing covering that middle clause.
You also put the file in tests/overrides/detectors/ rather than where the issue said, which is the right call since it mirrors the source path and sits with its siblings. Good instinct to check the layout instead of following the instructions literally.
Two small things and then this is ready.
|
|
||
| describe("installCmd", () => { | ||
| it("returns 'pnpm add' for pnpm", () => { | ||
| expect(installCmd("pnpm" as OverrideContext["packageManager"])).toBe( |
There was a problem hiding this comment.
These five casts are not doing anything. PackageManager is already "npm" | "pnpm" | "yarn" | "bun" | "unknown" (src/overrides/context.ts:14), so every string you pass is already a member of the union. I checked by deleting all five plus the then-unused import on line 5, and tsc reports no errors.
Worth removing rather than leaving, because the cast suppresses the one error you would actually want. If someone later drops "unknown" or renames "bun", these tests keep compiling and the drift goes unnoticed. Uncast, the build breaks and tells you, which for a file whose whole job is to pin this function's contract is the behaviour you want.
It also collapses each assertion back to one line:
expect(installCmd("pnpm")).toBe("pnpm add");| }; | ||
| const result = getDeclaredPackages(pkg); | ||
| expect(result).toEqual(new Set(["jest"])); | ||
| }); |
There was a problem hiding this comment.
One case missing, and it is the only real gap the mutation run found. I added "overrides" to the section list in getDeclaredPackages and all 12 tests still passed.
It matters slightly more than it looks, because this function decides whether a package counts as declared for PD001 (high severity, and it carries an override) and PD002. A real package.json has overrides, resolutions and scripts sitting at the same level as the four sections that should count.
Something like:
it("ignores sections that are not dependency sections", () => {
const pkg = {
dependencies: { react: "^18.0.0" },
overrides: { lodash: "4.17.21" },
resolutions: { minimist: "1.2.8" },
scripts: { build: "tsc" },
};
expect(getDeclaredPackages(pkg)).toEqual(new Set(["react"]));
});
What does this PR do?
Adds unit tests for the two pure helper functions in
src/overrides/detectors/phantom-utils.tsthat previously had no coverage, as requested in #1088.Which issue does this PR fix?
Closes #1088
Changes
tests/overrides/detectors/phantom-utils.test.ts(new): 12 tests total.getDeclaredPackages(7 tests)package.jsonnull, or stringsinstallCmd(5 tests)pnpm addfor pnpmyarn addfor yarnbun addfor bunnpm installfor npmnpm installas the default for an unknown package managerVerification
Ran the full CI-matching sequence locally on Ubuntu with Node.js v22.22.1:
All checks passed. The full test suite shows 1828 passed, 2 flaky (network-dependent tests in
tests/cli-integration.test.ts, confirmed passing on re-run).Test hygiene
describe/itstyle fromtests/overrides/detectors/oa001.test.ts.jsextension as required by the project's ESM setupnpm run lint:tests)