Skip to content

Commit db9fa0b

Browse files
authored
test: use Vitest for testing (#564)
* 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]>
1 parent 878c102 commit db9fa0b

8 files changed

+715
-2346
lines changed

jest.config.json

-20
This file was deleted.

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@
66
"build": "rollup -c",
77
"format": "prettier --write --cache . !dist !README.md",
88
"lint": "eslint",
9-
"test": "jest"
9+
"test": "vitest"
1010
},
1111
"dependencies": {
1212
"gha-utils": "^0.4.1"
1313
},
1414
"devDependencies": {
1515
"@eslint/js": "^9.17.0",
16-
"@jest/globals": "^29.7.0",
1716
"@rollup/plugin-node-resolve": "^16.0.0",
1817
"@rollup/plugin-typescript": "^12.1.2",
19-
"@types/jest": "^29.5.14",
2018
"@types/node": "^22.10.2",
19+
"@vitest/coverage-v8": "^2.1.8",
2120
"eslint": "^9.17.0",
22-
"jest": "^29.7.0",
2321
"prettier": "^3.4.2",
2422
"rollup": "^4.29.1",
25-
"ts-jest": "^29.2.5",
2623
"tslib": "^2.8.1",
2724
"typescript": "^5.7.2",
28-
"typescript-eslint": "^8.19.0"
25+
"typescript-eslint": "^8.19.0",
26+
"vitest": "^2.1.8"
2927
},
3028
"packageManager": "[email protected]"
3129
}

src/cmake.test.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { jest } from "@jest/globals";
1+
import { describe, expect, it, vi } from "vitest";
2+
import { buildProject, configureProject } from "./cmake.js";
23
import type { Context } from "./context.js";
4+
import { exec } from "./exec.js";
35

46
interface TestCase {
57
name: string;
@@ -21,9 +23,7 @@ const defaultContext: Context = {
2123
},
2224
};
2325

24-
jest.unstable_mockModule("./exec.js", () => ({
25-
exec: jest.fn(),
26-
}));
26+
vi.mock("./exec.js", () => ({ exec: vi.fn() }));
2727

2828
describe("configure a CMake project", () => {
2929
const testCases: TestCase[] = [
@@ -100,10 +100,7 @@ describe("configure a CMake project", () => {
100100

101101
for (const testCase of testCases) {
102102
it(`should execute the correct command ${testCase.name}`, async () => {
103-
const { configureProject } = await import("./cmake.js");
104-
const { exec } = await import("./exec.js");
105-
106-
jest.mocked(exec).mockReset();
103+
vi.mocked(exec).mockReset();
107104

108105
await configureProject({ ...defaultContext, ...testCase.context });
109106

@@ -144,10 +141,7 @@ describe("build a CMake project", () => {
144141

145142
for (const testCase of testCases) {
146143
it(`should execute the correct command ${testCase.name}`, async () => {
147-
const { buildProject } = await import("./cmake.js");
148-
const { exec } = await import("./exec.js");
149-
150-
jest.mocked(exec).mockReset();
144+
vi.mocked(exec).mockReset();
151145

152146
await buildProject({ ...defaultContext, ...testCase.context });
153147

src/context.test.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { jest } from "@jest/globals";
21
import path from "node:path";
2+
import { describe, expect, it, vi } from "vitest";
33
import type { Context } from "./context.js";
44

5-
jest.unstable_mockModule("gha-utils", () => ({
6-
getInput: jest.fn(),
7-
}));
5+
vi.mock("gha-utils", () => ({ getInput: vi.fn() }));
86

97
describe("get action context", () => {
108
interface TestCase {
@@ -186,9 +184,7 @@ describe("get action context", () => {
186184
const { getContext } = await import("./context.js");
187185

188186
const inputs = testCase.inputs || {};
189-
jest.mocked(getInput).mockImplementation((name) => {
190-
return inputs[name] || "";
191-
});
187+
vi.mocked(getInput).mockImplementation((name) => inputs[name] ?? "");
192188

193189
expect(getContext()).toStrictEqual({
194190
sourceDir: "",

src/exec.test.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import { jest } from "@jest/globals";
1+
import { logCommand } from "gha-utils";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { exec } from "./exec.js";
24

35
describe("execute commands", () => {
4-
const logCommand = jest.fn<(command: string, ...args: string[]) => void>();
5-
jest.unstable_mockModule("gha-utils", () => ({ logCommand }));
6+
vi.mock("gha-utils", () => ({
7+
logCommand: vi.fn<(command: string, ...args: string[]) => void>(),
8+
}));
69

710
beforeEach(() => {
8-
logCommand.mockClear();
11+
vi.mocked(logCommand).mockClear();
912
});
1013

1114
it("should successfully execute a command", async () => {
12-
const { exec } = await import("./exec.js");
13-
1415
await exec("node", ["--version"]);
1516

1617
expect(logCommand).toHaveBeenCalledTimes(1);
1718
expect(logCommand).toHaveBeenCalledWith("node", "--version");
1819
});
1920

2021
it("should fail to execute a command", async () => {
21-
const { exec } = await import("./exec.js");
22-
2322
await expect(exec("node", ["--invalid"])).rejects.toThrow(
2423
"Command exited with status code 9",
2524
);

tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"strict": true,
77
"module": "node16",
88
"moduleResolution": "node16",
9-
"esModuleInterop": true,
109
"target": "es2022",
1110
"skipLibCheck": true
1211
}

vitest.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
watch: false,
6+
coverage: {
7+
all: false,
8+
enabled: true,
9+
reporter: ["text"],
10+
thresholds: { 100: true },
11+
},
12+
},
13+
});

0 commit comments

Comments
 (0)