diff --git a/src/components/__tests__/add-command.test.ts b/src/components/__tests__/add-command.test.ts new file mode 100644 index 0000000..cea5e30 --- /dev/null +++ b/src/components/__tests__/add-command.test.ts @@ -0,0 +1,48 @@ +import { spawnSync } from "child_process"; +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from "@jest/globals"; +import { createAddCommand } from "../commands/add"; + +jest.mock("child_process", () => ({ + spawnSync: jest.fn(), +})); + +describe("components add command", () => { + const mockedSpawnSync = spawnSync as unknown as jest.Mock; + let logSpy: jest.SpiedFunction; + + beforeEach(() => { + mockedSpawnSync.mockReset(); + mockedSpawnSync.mockReturnValue({ status: 0 }); + logSpy = jest.spyOn(console, "log").mockImplementation(() => {}); + }); + + afterEach(() => { + logSpy.mockRestore(); + }); + + it("checks npx availability through the shell for Windows command resolution", async () => { + const command = createAddCommand(); + + await command.parseAsync(["bar-visualizer"], { from: "user" }); + + expect(mockedSpawnSync).toHaveBeenNthCalledWith(1, "npx", ["--version"], { + encoding: "utf-8", + shell: true, + }); + expect(mockedSpawnSync).toHaveBeenNthCalledWith( + 2, + "npx -y shadcn@latest add https://ui.elevenlabs.io/r/bar-visualizer.json", + { + stdio: "inherit", + shell: true, + } + ); + }); +}); diff --git a/src/components/commands/add.ts b/src/components/commands/add.ts index 4ccf83d..f6f3443 100644 --- a/src/components/commands/add.ts +++ b/src/components/commands/add.ts @@ -8,8 +8,8 @@ export function createAddCommand(): Command { .argument('[name]', 'Name of the component to add (optional)') .action(async (componentName?: string) => { try { - // Check if npx is available - const npxCheck = spawnSync('npx', ['--version'], { encoding: 'utf-8' }); + // Check if npx is available. shell:true lets Windows resolve npx.cmd/npx.ps1. + const npxCheck = spawnSync('npx', ['--version'], { encoding: 'utf-8', shell: true }); if (npxCheck.error) { console.error('Error: npx is not available. Please install Node.js/npm.'); process.exit(1);