Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests
Browse files Browse the repository at this point in the history
Thomas-Avery committed Nov 29, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c5b5be5 commit 5b023e2
Showing 4 changed files with 113 additions and 40 deletions.
13 changes: 13 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as main from "../src/main";

// Mock the action's entrypoint
const runMock = jest.spyOn(main, "run").mockImplementation();

describe("index", () => {
it("calls run when imported", async () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require("../src/index");

expect(runMock).toHaveBeenCalled();
});
});
48 changes: 48 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as core from "@actions/core";
import * as main from "../src/main";

// Mock the action's main function
const runMock = jest.spyOn(main, "run");

// Mock the GitHub Actions core library
let errorMock: jest.SpyInstance;
let getInputMock: jest.SpyInstance;
let getMultilineInput: jest.SpyInstance;
let setFailedMock: jest.SpyInstance;

describe("action", () => {
beforeEach(() => {
jest.clearAllMocks();

errorMock = jest.spyOn(core, "error").mockImplementation();
getInputMock = jest.spyOn(core, "getInput").mockImplementation();
getMultilineInput = jest.spyOn(core, "getMultilineInput").mockImplementation();
setFailedMock = jest.spyOn(core, "setFailed").mockImplementation();
});

it("sets a failed status", async () => {
getMultilineInput.mockImplementation((name: string): string[] => {
switch (name) {
default:
return [];
}
});

getInputMock.mockImplementation((name: string): string => {
switch (name) {
default:
return "";
}
});

await main.run();
expect(runMock).toHaveReturned();

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
"input provided for identity_url not in expected format",
);
expect(errorMock).not.toHaveBeenCalled();
});
});
91 changes: 51 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
"@bitwarden/sdk-napi": "0.3.1"
},
"devDependencies": {
"@types/jest": "29.5.10",
"@types/node": "20.10.0",
"@typescript-eslint/eslint-plugin": "6.13.1",
"@typescript-eslint/parser": "6.13.1",

0 comments on commit 5b023e2

Please sign in to comment.