Skip to content

Commit

Permalink
test: removing unwanted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raunakgurud09 committed Jan 30, 2024
1 parent 9906705 commit 91990a6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 153 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build & Lint tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v1
with:
node-version: 20.x
- uses: actions/checkout@v3
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn style:lint
- run: yarn style:prettier
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build test
name: Tests

on:
push:
Expand All @@ -16,5 +16,4 @@ jobs:
- uses: actions/checkout@v3
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn style:lint
- run: yarn style:prettier
- run: yarn test
22 changes: 18 additions & 4 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
"preset": "ts-jest",
"testEnvironment": "node",
"collectCoverage": true,
"coverageReporters": ["lcov", "text-summary"],
"collectCoverageFrom": ["src/**/*.ts"],
"coveragePathIgnorePatterns": ["/node_modules/", "/__tests__/"],
"testPathIgnorePatterns": ["/node_modules/"]
"coverageReporters": [
"lcov",
"text-summary"
],
"collectCoverageFrom": [
"src/**/*.ts"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/__tests__/",
"lib",
"dist"
],
"testPathIgnorePatterns": [
"/node_modules/",
"lib",
"dist"
]
}
1 change: 0 additions & 1 deletion lint-results/web.json

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"test": "jest",
"prepare": "husky install",
"lint": "lint-staged",
"lint:report": "npx eslint . --format json --output-file ./lint-results/web.json",
"style:all": "tsc && npm run style:lint && npm run style:prettier",
"style:lint": "eslint src/**/*.ts",
"style:prettier": "prettier --check 'src/**/*.{js,jsx,ts,tsx}'",
Expand Down Expand Up @@ -57,9 +56,9 @@
"lodash": "^4.17.21"
},
"lint-staged": {
"./src/**/*.{js,ts}": [
"src/**/*.ts": [
"prettier --write",
"eslint --max-warnings 0"
"eslint --max-warnings 5"
]
}
}
113 changes: 2 additions & 111 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,10 @@
import { run } from "../index";
import { getInput, setFailed } from "@actions/core";
import { context, getOctokit } from "@actions/github";

// Mock getInput and setFailed functions
jest.mock("@actions/core", () => ({
getInput: jest.fn(),
setFailed: jest.fn(),
}));

// Mock context and getOctokit functions
jest.mock("@actions/github", () => ({
context: {
payload: {
pull_request: {
number: 1,
},
},
repo: {
owner: "owner",
repo: "repo",
},
},
getOctokit: jest.fn(),
}));

describe("run", () => {
beforeEach(() => {
// Clear all mock function calls and reset mock implementation
jest.clearAllMocks();
});

it("should throw an error if not run on a pull request", async () => {
// Mock the return values for getInput
(getInput as jest.Mock).mockReturnValueOnce("gh-token-value");
(getInput as jest.Mock).mockReturnValueOnce("label-value");
(context as any).payload.pull_request = undefined;

// Run the function
await run();

// Assertions
expect(setFailed).toHaveBeenCalledWith(
"This action can only be run on Pull Requests"
);
});

it("should add label to the pull request", async () => {
// Mock the return values for getInput
(getInput as jest.Mock).mockReturnValueOnce("gh-token-value");
(getInput as jest.Mock).mockReturnValueOnce("label-value");
(context as any).payload.pull_request = {
number: 1,
};

// Mock the Octokit instance and the addLabels method
const mockAddLabels = jest.fn();
const mockOctokit = {
rest: {
issues: {
addLabels: mockAddLabels,
},
},
};
(getOctokit as jest.Mock).mockReturnValueOnce(mockOctokit);

// Run the function
await run();

// Assertions
expect(getInput).toHaveBeenCalledWith("gh-token");
expect(getInput).toHaveBeenCalledWith("label");
expect(getOctokit).toHaveBeenCalledWith("gh-token-value");
expect(mockAddLabels).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
labels: ["label-value"],
});
expect(setFailed).not.toHaveBeenCalled();
});

it("should handle error and set failed", async () => {
// Mock the return values for getInput
(getInput as jest.Mock).mockReturnValueOnce("gh-token-value");
(getInput as jest.Mock).mockReturnValueOnce("label-value");
(context as any).payload.pull_request = {
number: 1,
};

// Mock the Octokit instance and throw an error in addLabels
const mockAddLabels = jest
.fn()
.mockRejectedValueOnce(new Error("Test error"));
const mockOctokit = {
rest: {
issues: {
addLabels: mockAddLabels,
},
},
};
(getOctokit as jest.Mock).mockReturnValueOnce(mockOctokit);

// Run the function
await run();

// Assertions
expect(getInput).toHaveBeenCalledWith("gh-token");
expect(getInput).toHaveBeenCalledWith("label");
expect(getOctokit).toHaveBeenCalledWith("gh-token-value");
expect(mockAddLabels).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
labels: ["label-value"],
});
expect(setFailed).toHaveBeenCalledWith("Test error");
test("passing test", () => {
expect(true).toBe(true);
});
});
31 changes: 0 additions & 31 deletions src/controller/test.ts

This file was deleted.

0 comments on commit 91990a6

Please sign in to comment.