mirror of
https://github.com/threeal/cmake-action.git
synced 2026-02-01 19:40:49 +00:00
test: use Vitest for testing (#564)
Some checks are pending
Build / Build Package (push) Waiting to run
Check / Check Package (push) Waiting to run
Test / Test Package (push) Waiting to run
Test / Test Action (macos-14) (push) Waiting to run
Test / Test Action (ubuntu-24.04) (push) Waiting to run
Test / Test Action (windows-2022) (push) Waiting to run
Test / Test Action With Specified Directories (push) Waiting to run
Test / Test Action Without Run Build (push) Waiting to run
Test / Test Action With Additional Options (push) Waiting to run
Test / Test Action With Custom Generator (push) Waiting to run
Some checks are pending
Build / Build Package (push) Waiting to run
Check / Check Package (push) Waiting to run
Test / Test Package (push) Waiting to run
Test / Test Action (macos-14) (push) Waiting to run
Test / Test Action (ubuntu-24.04) (push) Waiting to run
Test / Test Action (windows-2022) (push) Waiting to run
Test / Test Action With Specified Directories (push) Waiting to run
Test / Test Action Without Run Build (push) Waiting to run
Test / Test Action With Additional Options (push) Waiting to run
Test / Test Action With Custom Generator (push) Waiting to run
* test: use Vitest for testing Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com> * build: remove `esModuleInterop` option in TypeScript configuration Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com> --------- Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { jest } from "@jest/globals";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { buildProject, configureProject } from "./cmake.js";
|
||||
import type { Context } from "./context.js";
|
||||
import { exec } from "./exec.js";
|
||||
|
||||
interface TestCase {
|
||||
name: string;
|
||||
@@ -21,9 +23,7 @@ const defaultContext: Context = {
|
||||
},
|
||||
};
|
||||
|
||||
jest.unstable_mockModule("./exec.js", () => ({
|
||||
exec: jest.fn(),
|
||||
}));
|
||||
vi.mock("./exec.js", () => ({ exec: vi.fn() }));
|
||||
|
||||
describe("configure a CMake project", () => {
|
||||
const testCases: TestCase[] = [
|
||||
@@ -100,10 +100,7 @@ describe("configure a CMake project", () => {
|
||||
|
||||
for (const testCase of testCases) {
|
||||
it(`should execute the correct command ${testCase.name}`, async () => {
|
||||
const { configureProject } = await import("./cmake.js");
|
||||
const { exec } = await import("./exec.js");
|
||||
|
||||
jest.mocked(exec).mockReset();
|
||||
vi.mocked(exec).mockReset();
|
||||
|
||||
await configureProject({ ...defaultContext, ...testCase.context });
|
||||
|
||||
@@ -144,10 +141,7 @@ describe("build a CMake project", () => {
|
||||
|
||||
for (const testCase of testCases) {
|
||||
it(`should execute the correct command ${testCase.name}`, async () => {
|
||||
const { buildProject } = await import("./cmake.js");
|
||||
const { exec } = await import("./exec.js");
|
||||
|
||||
jest.mocked(exec).mockReset();
|
||||
vi.mocked(exec).mockReset();
|
||||
|
||||
await buildProject({ ...defaultContext, ...testCase.context });
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { jest } from "@jest/globals";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { Context } from "./context.js";
|
||||
|
||||
jest.unstable_mockModule("gha-utils", () => ({
|
||||
getInput: jest.fn(),
|
||||
}));
|
||||
vi.mock("gha-utils", () => ({ getInput: vi.fn() }));
|
||||
|
||||
describe("get action context", () => {
|
||||
interface TestCase {
|
||||
@@ -186,9 +184,7 @@ describe("get action context", () => {
|
||||
const { getContext } = await import("./context.js");
|
||||
|
||||
const inputs = testCase.inputs || {};
|
||||
jest.mocked(getInput).mockImplementation((name) => {
|
||||
return inputs[name] || "";
|
||||
});
|
||||
vi.mocked(getInput).mockImplementation((name) => inputs[name] ?? "");
|
||||
|
||||
expect(getContext()).toStrictEqual({
|
||||
sourceDir: "",
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { jest } from "@jest/globals";
|
||||
import { logCommand } from "gha-utils";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { exec } from "./exec.js";
|
||||
|
||||
describe("execute commands", () => {
|
||||
const logCommand = jest.fn<(command: string, ...args: string[]) => void>();
|
||||
jest.unstable_mockModule("gha-utils", () => ({ logCommand }));
|
||||
vi.mock("gha-utils", () => ({
|
||||
logCommand: vi.fn<(command: string, ...args: string[]) => void>(),
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
logCommand.mockClear();
|
||||
vi.mocked(logCommand).mockClear();
|
||||
});
|
||||
|
||||
it("should successfully execute a command", async () => {
|
||||
const { exec } = await import("./exec.js");
|
||||
|
||||
await exec("node", ["--version"]);
|
||||
|
||||
expect(logCommand).toHaveBeenCalledTimes(1);
|
||||
@@ -18,8 +19,6 @@ describe("execute commands", () => {
|
||||
});
|
||||
|
||||
it("should fail to execute a command", async () => {
|
||||
const { exec } = await import("./exec.js");
|
||||
|
||||
await expect(exec("node", ["--invalid"])).rejects.toThrow(
|
||||
"Command exited with status code 9",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user