From 86cf39daf74b0b8aeb44649cfbca8d2453a857ce Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sat, 7 Dec 2024 10:39:58 +0100 Subject: [PATCH] test(cli): initial tests for 'prepare' --- packages/cli/commands/prepare.mjs | 25 +++++++++++++++++-------- packages/cli/test/prepare.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 packages/cli/test/prepare.test.js diff --git a/packages/cli/commands/prepare.mjs b/packages/cli/commands/prepare.mjs index f763c90..ebba99c 100755 --- a/packages/cli/commands/prepare.mjs +++ b/packages/cli/commands/prepare.mjs @@ -2,20 +2,15 @@ import { consola } from "consola"; import { defineCommand } from "citty"; -import { execSync as exec } from "node:child_process"; +import { execSync } from "node:child_process"; -export default defineCommand({ - meta: { - name: "prepare", - description: "Prepares the McFly workspace.", - }, - async run() { +function prepare() { consola.start("Preparing McFly workspace..."); let err; try { - exec("npx nitropack prepare", { stdio: "inherit" }); + execSync("npx nitropack prepare", { stdio: "inherit" }); } catch (e) { consola.error(e); err = e; @@ -26,5 +21,19 @@ export default defineCommand({ "McFly workspace preparation failed. Please make sure dependencies are installed.\n" ); } else consola.success("Done\n"); +} + +export default defineCommand({ + meta: { + name: "prepare", + description: "Prepares the McFly workspace.", + }, + run() { + prepare(); }, }); + + +export const exportedForTest = { + prepare +} \ No newline at end of file diff --git a/packages/cli/test/prepare.test.js b/packages/cli/test/prepare.test.js new file mode 100644 index 0000000..bf1a972 --- /dev/null +++ b/packages/cli/test/prepare.test.js @@ -0,0 +1,31 @@ +import { test } from "vitest"; +import { exportedForTest } from "../commands/prepare.mjs"; +import { vi } from "vitest"; +import consola from "consola"; +import { expect } from "vitest"; +import {execSync} from "node:child_process"; + +const testFn = exportedForTest.prepare; + +test('start prepare script', () => { + const spy = vi.spyOn(consola, 'start'); + + testFn(); + + expect(spy).toHaveBeenCalled() +}) + +test('execute nitropack prepare', () => { + const command = 'npx nitropack prepare'; + const param = {stdio: 'inherit'}; + vi.mock('node:child_process'); + + testFn(); + + expect(execSync).toHaveBeenCalled(); +}) + +/** + * TODO: + * - add test for catch error + */ \ No newline at end of file