From 47fa380ef042ae2f7074c1ed11da26949e000e4a Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sat, 7 Dec 2024 19:25:29 +0100 Subject: [PATCH] test(cli): add tests for serve() --- packages/cli/commands/serve.mjs | 53 +++++++++++++++++++-------------- packages/cli/test/serve.test.js | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 packages/cli/test/serve.test.js diff --git a/packages/cli/commands/serve.mjs b/packages/cli/commands/serve.mjs index 3a3d57a..53ae310 100644 --- a/packages/cli/commands/serve.mjs +++ b/packages/cli/commands/serve.mjs @@ -3,35 +3,44 @@ import { consola } from "consola"; import { colorize } from "consola/utils"; import { defineCommand } from "citty"; -import { execSync as exec } from "node:child_process"; +import { execSync } from "node:child_process"; import { createRequire } from "node:module"; +async function printInfo() { + try { + const _require = createRequire(import.meta.url); + const mcflyPkg = await _require("@mcflyjs/core/package.json"); + const mcflyPkgVersion = `McFly ${colorize("bold", mcflyPkg.version)}`; + const nitroPkg = await _require("nitropack/package.json"); + const nitroPkgVersion = `Nitro ${nitroPkg.version}`; + consola.log( + `${colorize("blue", mcflyPkgVersion)} ${colorize("dim", nitroPkgVersion)}` + ); + } catch (e) { + consola.error(e); + } +} + +function serve() { + try { + execSync(`npx nitropack dev`, { stdio: "inherit" }); + } catch (e) { + consola.error(e); + } +} + export default defineCommand({ meta: { name: "prepare", description: "Runs the dev server.", }, async run() { - try { - const _require = createRequire(import.meta.url); - const mcflyPkg = await _require("@mcflyjs/core/package.json"); - const mcflyPkgVersion = `McFly ${colorize("bold", mcflyPkg.version)}`; - const nitroPkg = await _require("nitropack/package.json"); - const nitroPkgVersion = `Nitro ${nitroPkg.version}`; - consola.log( - `${colorize("blue", mcflyPkgVersion)} ${colorize( - "dim", - nitroPkgVersion - )}` - ); - } catch (e) { - consola.error(e); - } - - try { - exec(`npx nitropack dev`, { stdio: "inherit" }); - } catch (e) { - consola.error(e); - } + await printInfo(); + serve(); }, }); + +export const exportedForTest = { + serve, + printInfo, +}; diff --git a/packages/cli/test/serve.test.js b/packages/cli/test/serve.test.js new file mode 100644 index 0000000..7b87f70 --- /dev/null +++ b/packages/cli/test/serve.test.js @@ -0,0 +1,53 @@ +import { describe, expect, test, vi } from "vitest"; +import { exportedForTest } from "../commands/serve.mjs"; +import consola from "consola"; + +describe("FUNCTION: serve()", () => { + const testFn = exportedForTest.serve; + const mocks = vi.hoisted(() => { + return { + execSync: vi.fn(), + }; + }); + + vi.mock("node:child_process", () => { + return { + execSync: mocks.execSync, + }; + }); + + test("execute nitropack serve", async () => { + const command = `npx nitropack dev`; + const param = { stdio: "inherit" }; + + testFn(); + + expect(mocks.execSync).toHaveBeenCalledWith(command, param); + }); + + test("catch error", () => { + const spy = vi.spyOn(consola, "error"); + mocks.execSync.mockImplementationOnce(() => { + throw new Error("hey"); + }); + + testFn(); + + expect(spy).toHaveBeenCalledWith(new Error("hey")); + }); +}); + +/** + * TODO: test printInfo + */ +// describe("FUNCTION: printInfo()", () => { +// const testFn = exportedForTest.printInfo; + +// test("log mcfly and nitro versions", () => { +// const spy = vi.spyOn(consola, "log"); + +// testFn(); + +// expect(spy).toHaveBeenCalled(); +// }); +// });