Skip to content

Commit

Permalink
test: reintroduce unit testing using Jest (#122)
Browse files Browse the repository at this point in the history
* test: add test for `yarn.disableGlobalCache` function

* test(eslint): add configuration for test files

* ci(test): add `test-package` job

* test: add test for `yarn.enable` function

* test: add test for `yarn.install` function
  • Loading branch information
threeal authored Feb 17, 2024
1 parent c728973 commit b747ed6
Show file tree
Hide file tree
Showing 6 changed files with 2,587 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"env": {
"node": true
}
},
{
"files": ["**/*.test.*"],
"env": {
"jest": true
}
}
]
}
20 changes: 20 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ jobs:
- name: Check Lint
run: corepack 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: Install Dependencies
uses: threeal/yarn-install-action@v1.0.0

- name: Test Package
run: corepack yarn test
env:
NODE_OPTIONS: --experimental-vm-modules

test-action:
name: Test Action
runs-on: ${{ matrix.os }}-latest
Expand Down
3 changes: 3 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"testMatch": ["**/*.test.*"]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"build": "ncc build src/index.mjs",
"format": "prettier --write --cache . !dist",
"lint": "eslint --ignore-path .gitignore ."
"lint": "eslint --ignore-path .gitignore .",
"test": "jest"
},
"dependencies": {
"@actions/cache": "^3.2.4",
Expand All @@ -15,6 +16,7 @@
"devDependencies": {
"@vercel/ncc": "^0.38.1",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"prettier": "^3.2.5"
},
"packageManager": "yarn@4.1.0"
Expand Down
52 changes: 52 additions & 0 deletions src/yarn.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { jest } from "@jest/globals";

jest.unstable_mockModule("@actions/exec", () => ({
exec: jest.fn(),
}));

beforeEach(() => {
jest.clearAllMocks();
});

it("should disable Yarn global cache", async () => {
const { exec } = await import("@actions/exec");
const yarn = (await import("./yarn.mjs")).default;

await yarn.disableGlobalCache();

expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenCalledWith("corepack", [
"yarn",
"config",
"set",
"enableGlobalCache",
"false",
]);
});

it("should enable Yarn", async () => {
const { exec } = await import("@actions/exec");
const yarn = (await import("./yarn.mjs")).default;

await yarn.enable();

expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenCalledWith("corepack", ["enable", "yarn"]);
});

it("should install package using Yarn", async () => {
const { exec } = await import("@actions/exec");
const yarn = (await import("./yarn.mjs")).default;

await yarn.install();

expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenCalledWith("corepack", ["yarn", "install"], {
env: {
...process.env,
GITHUB_ACTIONS: "",
FORCE_COLOR: "true",
CI: "",
},
});
});
Loading

0 comments on commit b747ed6

Please sign in to comment.