diff --git a/src/commands/deps.ts b/src/commands/deps.ts index 5c25029..d5c66b4 100644 --- a/src/commands/deps.ts +++ b/src/commands/deps.ts @@ -29,7 +29,9 @@ export default defineCommand({ consola.info("Example: pkg:npm/lodash@4.17.21"); process.exit(1); } - consola.info(`No version specified, using latest: ${pkg.latestVersion}`); + if (!args.json) { + consola.info(`No version specified, using latest: ${pkg.latestVersion}`); + } const deps = await reg.fetchDependencies(name, pkg.latestVersion); outputDeps(name, pkg.latestVersion, deps, args.json); return; diff --git a/test/unit/deps-command.test.ts b/test/unit/deps-command.test.ts new file mode 100644 index 0000000..384cc22 --- /dev/null +++ b/test/unit/deps-command.test.ts @@ -0,0 +1,55 @@ +const { mockResolvePURL } = vi.hoisted(() => ({ + mockResolvePURL: vi.fn(), +})); + +vi.mock("../../src/commands/shared.ts", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, resolvePURL: mockResolvePURL }; +}); + +import { runCommand } from "citty"; +import consola from "consola"; +import depsCommand from "../../src/commands/deps.ts"; +import type { Dependency } from "../../src/core/types.ts"; + +const dependencies: Dependency[] = [ + { name: "example-dependency", requirements: "^1.0.0", scope: "runtime", optional: false }, +]; + +describe("deps command", () => { + beforeEach(() => { + mockResolvePURL.mockReturnValue([ + { + fetchPackage: async () => ({ latestVersion: "2.0.0" }), + fetchDependencies: async () => dependencies, + }, + "example", + "", + ]); + }); + + afterEach(() => { + vi.restoreAllMocks(); + mockResolvePURL.mockReset(); + }); + + it("keeps JSON output parseable when the version is inferred", async () => { + const info = vi.spyOn(consola, "info").mockImplementation(() => undefined); + const output = vi.spyOn(console, "log").mockImplementation(() => undefined); + + await runCommand(depsCommand, { rawArgs: ["pkg:test/example", "--json", "--no-cache"] }); + + const stdout = output.mock.calls.map(([value]) => String(value)).join("\n"); + expect(JSON.parse(stdout)).toEqual(dependencies); + expect(info).not.toHaveBeenCalled(); + }); + + it("keeps the inferred version notice in human output", async () => { + const info = vi.spyOn(consola, "info").mockImplementation(() => undefined); + vi.spyOn(consola, "log").mockImplementation(() => undefined); + + await runCommand(depsCommand, { rawArgs: ["pkg:test/example", "--no-cache"] }); + + expect(info).toHaveBeenCalledWith("No version specified, using latest: 2.0.0"); + }); +});