Skip to content

Commit

Permalink
test: use Vitest for testing (#564)
Browse files Browse the repository at this point in the history
* test: use Vitest for testing

Signed-off-by: Alfi Maulana <[email protected]>

* build: remove `esModuleInterop` option in TypeScript configuration

Signed-off-by: Alfi Maulana <[email protected]>

---------

Signed-off-by: Alfi Maulana <[email protected]>
  • Loading branch information
threeal authored Jan 3, 2025
1 parent 878c102 commit db9fa0b
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 2,346 deletions.
20 changes: 0 additions & 20 deletions jest.config.json

This file was deleted.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@
"build": "rollup -c",
"format": "prettier --write --cache . !dist !README.md",
"lint": "eslint",
"test": "jest"
"test": "vitest"
},
"dependencies": {
"gha-utils": "^0.4.1"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
"@jest/globals": "^29.7.0",
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-typescript": "^12.1.2",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.2",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^9.17.0",
"jest": "^29.7.0",
"prettier": "^3.4.2",
"rollup": "^4.29.1",
"ts-jest": "^29.2.5",
"tslib": "^2.8.1",
"typescript": "^5.7.2",
"typescript-eslint": "^8.19.0"
"typescript-eslint": "^8.19.0",
"vitest": "^2.1.8"
},
"packageManager": "[email protected]"
}
18 changes: 6 additions & 12 deletions src/cmake.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[] = [
Expand Down Expand Up @@ -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 });

Expand Down Expand Up @@ -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 });

Expand Down
10 changes: 3 additions & 7 deletions src/context.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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: "",
Expand Down
15 changes: 7 additions & 8 deletions src/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
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);
expect(logCommand).toHaveBeenCalledWith("node", "--version");
});

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",
);
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"strict": true,
"module": "node16",
"moduleResolution": "node16",
"esModuleInterop": true,
"target": "es2022",
"skipLibCheck": true
}
Expand Down
13 changes: 13 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
watch: false,
coverage: {
all: false,
enabled: true,
reporter: ["text"],
thresholds: { 100: true },
},
},
});
Loading

0 comments on commit db9fa0b

Please sign in to comment.