-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.test.js
89 lines (77 loc) · 3.41 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { expect, describe, it, beforeEach, afterEach, vi } from "vitest";
import fs from "fs";
import childProcess from "child_process";
vi.mock("fs");
vi.mock("child_process");
describe("index.js", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
// Reset process env and args
process.env.HUSKY_GIT_PARAMS = "COMMIT_EDITMSG";
delete process.argv[2];
});
afterEach(() => {
vi.restoreAllMocks();
vi.resetAllMocks();
vi.resetModules();
});
it.each([
["SPAN", "SPAN-1-proof-of-concept", "Initial commit✨", "SPAN-1 Initial commit✨"],
["PROJECT", "PROJECT-3-githook-test", "Add support for githooks", "PROJECT-3 Add support for githooks"],
["TAG", "TAG-5032-add-readme", "Add readme to project", "TAG-5032 Add readme to project"],
])(
"should add a prefix to my unprefixed commit message",
async (jiraPrefix, branchName, commitMessage, expected) => {
await expect(executeScript(jiraPrefix, branchName, commitMessage)).resolves.to.equal(expected);
},
);
it("should add a prefix to my unprefixed commit message by detecting the tag using regex", async () => {
process.env.TAG_MATCHER = "^[^/]+/(SPAN-[0-9]+)";
process.env.TAG_MATCH_INDEX = "1";
await expect(executeScript(undefined, "FEAT/SPAN-1234/init", "Initial commit")).resolves.to.equal(
"SPAN-1234 Initial commit",
);
delete process.env.TAG_MATCHER;
delete process.env.TAG_MATCH_INDEX;
});
it.each([
["SPAN", "SPAN-1-proof-of-concept", "SPAN-1 Initial commit✨", "SPAN-1 Initial commit✨"],
[
"PROJECT",
"PROJECT-3-githook-test",
"PROJECT-3 Add support for githooks",
"PROJECT-3 Add support for githooks",
],
["TAG", "TAG-5032-add-readme", "TAG-5032 Add readme to project", "TAG-5032 Add readme to project"],
])(
"should not add a prefix again to my already prefixed commit message",
async (jiraPrefix, branchName, commitMessage, expected) => {
await expect(executeScript(jiraPrefix, branchName, commitMessage)).resolves.to.equal(expected);
},
);
it("should not add a prefix in merge pull requests", async () => {
const commitMergeMessage =
"Merge branch 'develop' of github.com:jessedobbelaere/jira-smart-commit into bugfixes";
await expect(executeScript("SPAN", "SPAN-1-proof-of-concept", commitMergeMessage)).resolves.to.equal(
commitMergeMessage,
);
});
it("should not add a prefix if a branch was not prefixed", async () => {
await expect(executeScript("TAG", "conquer-the-world-PoC", "Initial commit")).resolves.to.equal(
"Initial commit",
);
});
const executeScript = async (jiraPrefix, branchName, commitMessage) => {
// Mock execution environment
process.env.HUSKY_GIT_PARAMS = "COMMIT_EDITMSG";
process.argv[2] = jiraPrefix; // Set CLI argument for JIRA prefix
vi.mocked(childProcess.execSync).mockReturnValue(branchName);
vi.mocked(fs.readFileSync).mockReturnValueOnce(commitMessage);
const writeFileMock = vi.mocked(fs.writeFileSync);
// Run script
const modulePath = require.resolve("./index.js");
await import(modulePath);
return writeFileMock.mock.calls[0][1];
};
});