mcfly/packages/cli/test/build.test.js
Ayo Ayco 56592bdeef
test(cli): add tests for cli commands (#37)
* chore(cli): add vitest & add test script

* chore: add test:cli script

* test(cli): initial tests for build command

* chore(cli): add nitropack as devDepencency for tests

* chore(cli): add vitest config

* test(cli): add tests for build command

* test(cli): add tests for new command
2024-12-07 02:44:38 +01:00

48 lines
958 B
JavaScript

import consola from "consola";
import { vi, expect, test } from "vitest";
import { exportedForTest } from "../commands/build.mjs";
const testFn = exportedForTest.build;
const mocks = vi.hoisted(() => {
return {
execSync: vi.fn()
}
})
vi.mock('node:child_process', () => {
return {
execSync: mocks.execSync
}
})
test("start build with message", () => {
const message = "Building project...";
const spy = vi.spyOn(consola, "start");
testFn();
expect(spy).toHaveBeenCalledWith(message);
});
test("execute nitropack build", () => {
const command = "npx nitropack build";
const param = { stdio: "inherit" };
testFn();
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'))
// testFn();
// expect(spy).toHaveBeenCalled();
// });