Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce Unit Testing Using Jest #122

Merged
merged 5 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading