From 0834f1363369f80523609856ba4bddefcb84aae2 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sat, 7 Dec 2024 18:35:34 +0100 Subject: [PATCH] test(cli): error scenarios for new, build, prepare --- packages/cli/test/build.test.js | 32 ++++++------ packages/cli/test/new.test.js | 85 +++++++++++++++++++------------ packages/cli/test/prepare.test.js | 33 +++++++++--- 3 files changed, 95 insertions(+), 55 deletions(-) diff --git a/packages/cli/test/build.test.js b/packages/cli/test/build.test.js index a05ff71..865ccf7 100644 --- a/packages/cli/test/build.test.js +++ b/packages/cli/test/build.test.js @@ -6,15 +6,15 @@ const testFn = exportedForTest.build; const mocks = vi.hoisted(() => { return { - execSync: vi.fn() - } -}) + execSync: vi.fn(), + }; +}); -vi.mock('node:child_process', () => { +vi.mock("node:child_process", () => { return { - execSync: mocks.execSync - } -}) + execSync: mocks.execSync, + }; +}); test("start build with message", () => { const message = "Building project..."; @@ -34,15 +34,13 @@ test("execute nitropack build", () => { expect(mocks.execSync).toHaveBeenCalledWith(command, param); }); -/** - * TODO - * - test catch error - */ -// test("catch error", () => { -// const spy = vi.spyOn(consola, "error"); -// mocks.execSync.mockImplementation(() => new Error('hey')) +test("catch error", () => { + const spy = vi.spyOn(consola, "error"); + mocks.execSync.mockImplementationOnce(() => { + throw new Error("hey"); + }); -// testFn(); + testFn(); -// expect(spy).toHaveBeenCalled(); -// }); + expect(spy).toHaveBeenCalledWith(new Error("hey")); +}); diff --git a/packages/cli/test/new.test.js b/packages/cli/test/new.test.js index 6daf923..f07cfd4 100644 --- a/packages/cli/test/new.test.js +++ b/packages/cli/test/new.test.js @@ -1,58 +1,79 @@ import { beforeAll, expect, test, vi } from "vitest"; import { exportedForTest } from "../commands/new.mjs"; import { execSync } from "node:child_process"; +import consola from "consola"; const testFn = exportedForTest.createNew; -const baseCommand = `npm create mcfly@latest` +const baseCommand = `npm create mcfly@latest`; -beforeAll(() => { - vi.mock('node:child_process'); -}) +const mocks = vi.hoisted(() => { + return { + execSync: vi.fn(), + }; +}); + +vi.mock("node:child_process", () => { + return { + execSync: mocks.execSync, + }; +}); 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", () => { - const dir = 'fake-dir' - const command = `${baseCommand} ${dir}` - const param = {stdio: 'inherit'} + const dir = "fake-dir"; + const command = `${baseCommand} ${dir}`; + 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", () => { - const dir = 'fake-dir' - const command = `${baseCommand} ${dir}` - const param = {stdio: 'inherit'} + const dir = "fake-dir"; + const command = `${baseCommand} ${dir}`; + const param = { stdio: "inherit" }; - testFn({dir}) + testFn({ dir }); - expect(execSync).toHaveBeenCalledWith(command,param) -}) + expect(execSync).toHaveBeenCalledWith(command, param); +}); test("execute create mcfly with _dir", () => { - const dir = 'fake-dir' - const command = `${baseCommand} ${dir}` - const param = {stdio: 'inherit'} + const dir = "fake-dir"; + const command = `${baseCommand} ${dir}`; + 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", () => { - const dir = 'preferred-dir' - const command = `${baseCommand} ${dir}` - const param = {stdio: 'inherit'} + const dir = "preferred-dir"; + const command = `${baseCommand} ${dir}`; + const param = { stdio: "inherit" }; - testFn({dir: dir, _dir: 'not-preferred'}) + testFn({ dir: dir, _dir: "not-preferred" }); - expect(execSync).toHaveBeenCalledWith(command,param) -}) \ No newline at end of file + 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")); +}); diff --git a/packages/cli/test/prepare.test.js b/packages/cli/test/prepare.test.js index 19d1904..1f95e5f 100644 --- a/packages/cli/test/prepare.test.js +++ b/packages/cli/test/prepare.test.js @@ -5,6 +5,18 @@ import { execSync } from "node:child_process"; 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", () => { const spy = vi.spyOn(consola, "start"); @@ -14,16 +26,25 @@ test("start prepare script", () => { }); test("execute nitropack prepare", () => { + const successSpy = vi.spyOn(consola, "success"); const command = "npx nitropack prepare"; const param = { stdio: "inherit" }; - vi.mock("node:child_process"); testFn(); - expect(execSync).toHaveBeenCalled(); + expect(execSync).toHaveBeenCalledWith(command, param); + expect(successSpy).toHaveBeenCalled(); }); -/** - * TODO: - * - add test for catch error - */ +test("catch error", () => { + const errSpy = vi.spyOn(consola, "error"); + const failSpy = vi.spyOn(consola, "fail"); + mocks.execSync.mockImplementationOnce(() => { + throw new Error(); + }); + + testFn(); + + expect(errSpy).toHaveBeenCalled(); + expect(failSpy).toHaveBeenCalled(); +});