test(cli): initial tests for 'prepare'

This commit is contained in:
Ayo Ayco 2024-12-07 10:39:58 +01:00
parent d4a5be60f1
commit 86cf39daf7
2 changed files with 48 additions and 8 deletions

View file

@ -2,20 +2,15 @@
import { consola } from "consola"; import { consola } from "consola";
import { defineCommand } from "citty"; import { defineCommand } from "citty";
import { execSync as exec } from "node:child_process"; import { execSync } from "node:child_process";
export default defineCommand({ function prepare() {
meta: {
name: "prepare",
description: "Prepares the McFly workspace.",
},
async run() {
consola.start("Preparing McFly workspace..."); consola.start("Preparing McFly workspace...");
let err; let err;
try { try {
exec("npx nitropack prepare", { stdio: "inherit" }); execSync("npx nitropack prepare", { stdio: "inherit" });
} catch (e) { } catch (e) {
consola.error(e); consola.error(e);
err = e; err = e;
@ -26,5 +21,19 @@ export default defineCommand({
"McFly workspace preparation failed. Please make sure dependencies are installed.\n" "McFly workspace preparation failed. Please make sure dependencies are installed.\n"
); );
} else consola.success("Done\n"); } else consola.success("Done\n");
}
export default defineCommand({
meta: {
name: "prepare",
description: "Prepares the McFly workspace.",
},
run() {
prepare();
}, },
}); });
export const exportedForTest = {
prepare
}

View file

@ -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
*/