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(() => {
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"));
});

View file

@ -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)
})
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 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();
});