test(cli): error scenarios for new, build, prepare

This commit is contained in:
Ayo Ayco 2024-12-07 18:35:34 +01:00
parent 025587c0e6
commit 0834f13633
3 changed files with 95 additions and 55 deletions

View file

@ -6,15 +6,15 @@ const testFn = exportedForTest.build;
const mocks = vi.hoisted(() => { const mocks = vi.hoisted(() => {
return { return {
execSync: vi.fn() execSync: vi.fn(),
} };
}) });
vi.mock('node:child_process', () => { vi.mock("node:child_process", () => {
return { return {
execSync: mocks.execSync execSync: mocks.execSync,
} };
}) });
test("start build with message", () => { test("start build with message", () => {
const message = "Building project..."; const message = "Building project...";
@ -34,15 +34,13 @@ test("execute nitropack build", () => {
expect(mocks.execSync).toHaveBeenCalledWith(command, param); expect(mocks.execSync).toHaveBeenCalledWith(command, param);
}); });
/** test("catch error", () => {
* TODO const spy = vi.spyOn(consola, "error");
* - test catch error mocks.execSync.mockImplementationOnce(() => {
*/ throw new Error("hey");
// test("catch error", () => { });
// const spy = vi.spyOn(consola, "error");
// mocks.execSync.mockImplementation(() => new Error('hey'))
// testFn(); testFn();
// expect(spy).toHaveBeenCalled(); expect(spy).toHaveBeenCalledWith(new Error("hey"));
// }); });

View file

@ -1,58 +1,79 @@
import { beforeAll, expect, test, vi } from "vitest"; import { beforeAll, expect, test, vi } from "vitest";
import { exportedForTest } from "../commands/new.mjs"; import { exportedForTest } from "../commands/new.mjs";
import { execSync } from "node:child_process"; import { execSync } from "node:child_process";
import consola from "consola";
const testFn = exportedForTest.createNew; const testFn = exportedForTest.createNew;
const baseCommand = `npm create mcfly@latest` const baseCommand = `npm create mcfly@latest`;
beforeAll(() => { const mocks = vi.hoisted(() => {
vi.mock('node:child_process'); return {
}) execSync: vi.fn(),
};
});
vi.mock("node:child_process", () => {
return {
execSync: mocks.execSync,
};
});
test("execute create mcfly", () => { test("execute create mcfly", () => {
const param = {stdio: 'inherit'} const param = { stdio: "inherit" };
testFn({dir: undefined}) testFn({ dir: undefined });
expect(execSync).toHaveBeenCalledWith(baseCommand, param) expect(execSync).toHaveBeenCalledWith(baseCommand, param);
}) });
test("execute create mcfly with no dir", () => { test("execute create mcfly with no dir", () => {
const dir = 'fake-dir' const dir = "fake-dir";
const command = `${baseCommand} ${dir}` const command = `${baseCommand} ${dir}`;
const param = {stdio: 'inherit'} const param = { stdio: "inherit" };
testFn({dir: undefined}) testFn({ dir: undefined });
expect(execSync).not.toHaveBeenCalledWith(command, param) expect(execSync).not.toHaveBeenCalledWith(command, param);
}) });
test("execute create mcfly with dir", () => { test("execute create mcfly with dir", () => {
const dir = 'fake-dir' const dir = "fake-dir";
const command = `${baseCommand} ${dir}` const command = `${baseCommand} ${dir}`;
const param = {stdio: 'inherit'} const param = { stdio: "inherit" };
testFn({dir}) testFn({ dir });
expect(execSync).toHaveBeenCalledWith(command,param) expect(execSync).toHaveBeenCalledWith(command, param);
}) });
test("execute create mcfly with _dir", () => { test("execute create mcfly with _dir", () => {
const dir = 'fake-dir' const dir = "fake-dir";
const command = `${baseCommand} ${dir}` const command = `${baseCommand} ${dir}`;
const param = {stdio: 'inherit'} const param = { stdio: "inherit" };
testFn({_dir: dir}) testFn({ _dir: dir });
expect(execSync).toHaveBeenCalledWith(command,param) expect(execSync).toHaveBeenCalledWith(command, param);
}) });
test("execute create mcfly with dir preferred over _dir", () => { test("execute create mcfly with dir preferred over _dir", () => {
const dir = 'preferred-dir' const dir = "preferred-dir";
const command = `${baseCommand} ${dir}` const command = `${baseCommand} ${dir}`;
const param = {stdio: 'inherit'} const param = { stdio: "inherit" };
testFn({dir: dir, _dir: 'not-preferred'}) testFn({ dir: dir, _dir: "not-preferred" });
expect(execSync).toHaveBeenCalledWith(command,param) expect(execSync).toHaveBeenCalledWith(command, param);
}) });
test("catch error", () => {
const dir = "fake-dir";
const spy = vi.spyOn(consola, "error");
mocks.execSync.mockImplementationOnce(() => {
throw new Error("hey");
});
testFn({ dir });
expect(spy).toHaveBeenCalledWith(new Error("hey"));
});

View file

@ -5,6 +5,18 @@ import { execSync } from "node:child_process";
const testFn = exportedForTest.prepare; const testFn = exportedForTest.prepare;
const mocks = vi.hoisted(() => {
return {
execSync: vi.fn(),
};
});
vi.mock("node:child_process", () => {
return {
execSync: mocks.execSync,
};
});
test("start prepare script", () => { test("start prepare script", () => {
const spy = vi.spyOn(consola, "start"); const spy = vi.spyOn(consola, "start");
@ -14,16 +26,25 @@ test("start prepare script", () => {
}); });
test("execute nitropack prepare", () => { test("execute nitropack prepare", () => {
const successSpy = vi.spyOn(consola, "success");
const command = "npx nitropack prepare"; const command = "npx nitropack prepare";
const param = { stdio: "inherit" }; const param = { stdio: "inherit" };
vi.mock("node:child_process");
testFn(); testFn();
expect(execSync).toHaveBeenCalled(); expect(execSync).toHaveBeenCalledWith(command, param);
expect(successSpy).toHaveBeenCalled();
}); });
/** test("catch error", () => {
* TODO: const errSpy = vi.spyOn(consola, "error");
* - add test for catch error const failSpy = vi.spyOn(consola, "fail");
*/ mocks.execSync.mockImplementationOnce(() => {
throw new Error();
});
testFn();
expect(errSpy).toHaveBeenCalled();
expect(failSpy).toHaveBeenCalled();
});