test: reimplement unit testing (#229)

* test: add test for `getInputs` function

* ci(test): add `test-package` job
This commit is contained in:
Alfi Maulana 2024-03-08 21:20:32 +07:00 committed by GitHub
parent a6cac00b5d
commit f948a9da23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 2661 additions and 14 deletions

1
.env.yarn Normal file
View File

@ -0,0 +1 @@
NODE_OPTIONS=--experimental-vm-modules

View File

@ -30,6 +30,26 @@ jobs:
- name: Check Lint
run: yarn lint
test-package:
name: Test Package
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4.1.1
- name: Setup Node.js
uses: actions/setup-node@v4.0.2
with:
node-version: latest
- name: Setup Yarn
uses: threeal/setup-yarn-action@v2.0.0
with:
version: stable
- name: Test Package
run: yarn test
test-action:
name: Test Action
runs-on: ${{ matrix.os }}-latest

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
.*
!.env.yarn
!.eslint*
!.git*
build/
coverage/
node_modules/

19
jest.config.json Normal file
View File

@ -0,0 +1,19 @@
{
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"preset": "ts-jest/presets/default-esm",
"transform": {
"^.+\\.ts$": ["ts-jest", { "useESM": true }]
},
"verbose": true
}

View File

@ -4,7 +4,8 @@
"scripts": {
"build": "ncc build src/index.ts",
"format": "prettier --write --cache . !dist !README.md",
"lint": "eslint --ignore-path .gitignore ."
"lint": "eslint --ignore-path .gitignore .",
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.10.1",
@ -12,12 +13,16 @@
"@actions/io": "^1.1.3"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.1",
"@vercel/ncc": "^0.38.1",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
},
"packageManager": "yarn@4.1.1"

128
src/inputs.test.ts Normal file
View File

@ -0,0 +1,128 @@
import { jest } from "@jest/globals";
jest.unstable_mockModule("@actions/core", () => ({
getBooleanInput: jest.fn(),
getInput: jest.fn(),
getMultilineInput: jest.fn(),
}));
describe("get action inputs", () => {
describe("with default values", () => {
beforeEach(async () => {
const { getBooleanInput, getInput, getMultilineInput } = await import(
"@actions/core"
);
jest.mocked(getBooleanInput).mockReturnValue(false);
jest.mocked(getInput).mockReturnValue("");
jest.mocked(getMultilineInput).mockReturnValue([]);
});
it("should get the action inputs", async () => {
const { getInputs } = await import("./inputs.js");
const inputs = getInputs();
expect(inputs).toStrictEqual({
sourceDir: ".",
buildDir: "build",
generator: "",
cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [],
args: [],
runBuild: false,
buildArgs: [],
runTest: false,
testArgs: [],
});
});
});
describe("with specified values", () => {
beforeEach(async () => {
const { getBooleanInput, getInput, getMultilineInput } = await import(
"@actions/core"
);
jest.mocked(getBooleanInput).mockImplementation((name) => {
switch (name) {
case "run-build":
return true;
case "run-test":
return true;
}
throw new Error(`invalid input name: ${name}`);
});
jest.mocked(getInput).mockImplementation((name) => {
switch (name) {
case "source-dir":
return "some-source";
case "build-dir":
return "some-build";
case "generator":
return "some-generator";
case "c-compiler":
return "some-c-compiler";
case "cxx-compiler":
return "some-cxx-compiler";
}
throw new Error(`invalid input name: ${name}`);
});
jest.mocked(getMultilineInput).mockImplementation((name) => {
switch (name) {
case "c-flags":
return ["some-c-flag another-c-flag", "some-other-c-flag"];
case "cxx-flags":
return ["some-cxx-flag another-cxx-flag", "some-other-cxx-flag"];
case "options":
return ["some-options another-options", "some-other-options"];
case "args":
return ["some-args another-args", "some-other-args"];
case "build-args":
return [
"some-build-args another-build-args",
"some-other-build-args",
];
case "test-args":
return ["some-test-args another-test-args", "some-other-test-args"];
}
throw new Error(`invalid input name: ${name}`);
});
});
it("should get the action inputs", async () => {
const { getInputs } = await import("./inputs.js");
const inputs = getInputs();
expect(inputs).toStrictEqual({
sourceDir: "some-source",
buildDir: "some-build",
generator: "some-generator",
cCompiler: "some-c-compiler",
cxxCompiler: "some-cxx-compiler",
cFlags: "some-c-flag another-c-flag some-other-c-flag",
cxxFlags: "some-cxx-flag another-cxx-flag some-other-cxx-flag",
options: ["some-options", "another-options", "some-other-options"],
args: ["some-args", "another-args", "some-other-args"],
runBuild: true,
buildArgs: [
"some-build-args",
"another-build-args",
"some-other-build-args",
],
runTest: true,
testArgs: [
"some-test-args",
"another-test-args",
"some-other-test-args",
],
});
});
});
});

2498
yarn.lock generated

File diff suppressed because it is too large Load Diff