🧹 Add basic unit tests (#148)

This is not intended to be a comprehensive test suite; I'm just trying to get us started by adding tests for some of the most important functionality.

This necessitated some minor refactoring. Previously, `main.ts` just directly invoked the main entrypoint function `run()`, which made it impossible to unit test the module without causing the side-effects of `run`. 

As a workaround I created  a new module `labeler.ts`, which just exports the functions we want to test without executing the main entrypoint, and simplified `main.ts` so that it only imports the entrypoint and executes it.

It's basically just a re-org. The diff makes it look way more complicated than it is.
This commit is contained in:
Patrick Ellis
2021-06-03 16:15:47 -04:00
committed by GitHub
parent ffa3fbeb8f
commit 9019323db1
4 changed files with 289 additions and 262 deletions

29
__tests__/labeler.test.ts Normal file
View File

@@ -0,0 +1,29 @@
import { checkGlobs } from '../src/labeler'
import * as core from "@actions/core";
jest.mock("@actions/core");
beforeAll(() => {
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
return jest.requireActual("@actions/core").getInput(name, options);
});
});
const matchConfig = [{ any: ["*.txt"] }];
describe('checkGlobs', () => {
it('returns true when our pattern does match changed files', () => {
const changedFiles = ["foo.txt", "bar.txt"];
const result = checkGlobs(changedFiles, matchConfig);
expect(result).toBeTruthy();
});
it('returns false when our pattern does not match changed files', () => {
const changedFiles = ["foo.docx"];
const result = checkGlobs(changedFiles, matchConfig);
expect(result).toBeFalsy();
});
});