Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/commands/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 55 additions & 0 deletions test/unit/deps-command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { mockResolvePURL } = vi.hoisted(() => ({
mockResolvePURL: vi.fn(),
}));

vi.mock("../../src/commands/shared.ts", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../src/commands/shared.ts")>();
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");
});
});
Loading