Skip to content

Commit 99b48dd

Browse files
authored
Refactor commit message to unit test (#94)
* Refactor commit message to unit test * Format * Update vitest config
1 parent d276c14 commit 99b48dd

8 files changed

Lines changed: 80 additions & 103 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ jobs:
6464
- name: Build
6565
run: pnpm build
6666

67+
- name: Unit tests
68+
run: pnpm test:unit
69+
6770
- name: Integration tests
6871
run: pnpm test:integration
6972
env:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"format": "oxfmt --check",
4141
"format:fix": "oxfmt",
4242
"lint": "oxlint",
43+
"test:unit": "vitest",
4344
"test:integration": "vitest -c vitest.integration.config.ts",
4445
"version-packages": "changeset version && pnpm format:fix",
4546
"release": "pnpm build && changeset publish"

src/core.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { GetRepositoryMetadataQuery } from "./github/graphql/generated/operations.js";
2-
import type { CommitMessage } from "./github/graphql/generated/types.ts";
32
import {
43
createCommitOnBranchQuery,
54
getRepositoryMetadata,
@@ -9,6 +8,7 @@ import type {
98
CommitFilesResult,
109
GitBase,
1110
} from "./interface.ts";
11+
import { normalizeCommitMessage } from "./utils.ts";
1212

1313
const getBaseRef = (base: GitBase): string => {
1414
if ("branch" in base) {
@@ -62,22 +62,14 @@ const createCommit = async ({
6262
refId: string;
6363
baseOid: string;
6464
}) => {
65-
const normalizedMessage: CommitMessage =
66-
typeof message === "string"
67-
? {
68-
headline: message.split("\n")[0]?.trim() ?? "",
69-
body: message.split("\n").slice(1).join("\n").trim(),
70-
}
71-
: message;
72-
7365
// we have to stick to GraphQL here as with REST, each file change would become a separate API call
7466
return createCommitOnBranchQuery(octokit, {
7567
input: {
7668
branch: {
7769
id: refId,
7870
},
7971
expectedHeadOid: baseOid,
80-
message: normalizedMessage,
72+
message: normalizeCommitMessage(message),
8173
fileChanges,
8274
},
8375
});

src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { CommitMessage } from "./github/graphql/generated/types.ts";
2+
3+
export function normalizeCommitMessage(
4+
message: string | CommitMessage,
5+
): CommitMessage {
6+
if (typeof message === "object") {
7+
return {
8+
headline: message.headline.trim(),
9+
body: message.body?.trim(),
10+
};
11+
}
12+
13+
if (!message.includes("\n")) {
14+
return { headline: message.trim() };
15+
}
16+
17+
const [headline, ...bodyLines] = message.split("\n");
18+
return {
19+
headline: headline.trim(),
20+
body: bodyLines.join("\n").trim(),
21+
};
22+
}

tests/integration/node.test.ts

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { promises as fs } from "fs";
22
import { getOctokit } from "@actions/github";
33
import git from "isomorphic-git";
44
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
5-
import type { CommitMessage } from "../../src/github/graphql/generated/types.ts";
65
import {
76
createRefMutation,
87
getRefTreeQuery,
@@ -497,97 +496,6 @@ describe("node", () => {
497496
});
498497
});
499498
});
500-
501-
describe("commit message is correctly handled", () => {
502-
const testCommitMessage = async ({
503-
branch,
504-
input,
505-
expected,
506-
}: {
507-
branch: string;
508-
input: string | CommitMessage;
509-
expected: string;
510-
}) => {
511-
await commitFilesFromBuffers({
512-
octokit,
513-
...REPO,
514-
branch,
515-
base: {
516-
commit: testTargetCommit,
517-
},
518-
...BASIC_FILE_CONTENTS,
519-
message: input,
520-
});
521-
522-
await waitForGitHubToBeReady();
523-
524-
const ref = (
525-
await getRefTreeQuery(octokit, {
526-
...REPO,
527-
ref: `refs/heads/${branch}`,
528-
path: "package.json",
529-
})
530-
).repository?.ref?.target;
531-
532-
if (!ref || !("message" in ref)) {
533-
throw new Error("Unexpected result");
534-
}
535-
536-
expect(ref.message).toEqual(expected);
537-
};
538-
539-
it("single string", async () => {
540-
const branch = `${TEST_BRANCH_PREFIX}-commit-message-single`;
541-
branches.push(branch);
542-
543-
await testCommitMessage({
544-
branch,
545-
input: "This is a basic commit message",
546-
expected: "This is a basic commit message",
547-
});
548-
});
549-
550-
it("multi-line string", async () => {
551-
const branch = `${TEST_BRANCH_PREFIX}-commit-message-multi`;
552-
branches.push(branch);
553-
554-
await testCommitMessage({
555-
branch,
556-
input:
557-
"This is a basic commit message\nwith a second line\n\nand some more lines!",
558-
expected:
559-
"This is a basic commit message\n\nwith a second line\n\nand some more lines!",
560-
});
561-
});
562-
563-
it("headline only", async () => {
564-
const branch = `${TEST_BRANCH_PREFIX}-commit-message-h-only`;
565-
branches.push(branch);
566-
567-
await testCommitMessage({
568-
branch,
569-
input: {
570-
headline: "This is a basic commit message!!",
571-
},
572-
expected: "This is a basic commit message!!",
573-
});
574-
});
575-
576-
it("headline & body", async () => {
577-
const branch = `${TEST_BRANCH_PREFIX}-commit-message-h-and-b`;
578-
branches.push(branch);
579-
580-
await testCommitMessage({
581-
branch,
582-
input: {
583-
headline: "This is a basic commit message!!",
584-
body: "This is the body of the commit message\nit has more text",
585-
},
586-
expected:
587-
"This is a basic commit message!!\n\nThis is the body of the commit message\nit has more text",
588-
});
589-
});
590-
});
591499
});
592500

593501
afterAll(async () => {

tests/utils.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "vitest";
2+
import { normalizeCommitMessage } from "../src/utils.ts";
3+
4+
describe("normalizeCommitMessage", () => {
5+
it("handles single line messages", () => {
6+
const message = "This is a commit message";
7+
const result = normalizeCommitMessage(message);
8+
expect(result).toEqual({ headline: "This is a commit message" });
9+
});
10+
11+
it("handles multi line messages", () => {
12+
const message =
13+
"This is a commit message\nwith a second line\nand a third line";
14+
const result = normalizeCommitMessage(message);
15+
expect(result).toEqual({
16+
headline: "This is a commit message",
17+
body: "with a second line\nand a third line",
18+
});
19+
});
20+
21+
it("trims whitespace from headline and body", () => {
22+
const message = " This is a commit message \n with a second line ";
23+
const result = normalizeCommitMessage(message);
24+
expect(result).toEqual({
25+
headline: "This is a commit message",
26+
body: "with a second line",
27+
});
28+
});
29+
30+
it("handles object messages", () => {
31+
const message = {
32+
headline: " This is a commit message ",
33+
body: " with a second line ",
34+
};
35+
const result = normalizeCommitMessage(message);
36+
expect(result).toEqual({
37+
headline: "This is a commit message",
38+
body: "with a second line",
39+
});
40+
});
41+
});

vitest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
experimental: { preParse: true },
6+
clearMocks: true,
7+
include: ["tests/*.test.ts"],
8+
},
9+
});

vitest.integration.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ process.env.ROOT_TEMP_DIRECTORY ??= path.join(
1616

1717
export default defineConfig({
1818
test: {
19-
environment: "node",
19+
experimental: { preParse: true },
20+
clearMocks: true,
2021
globalSetup: ["./tests/integration/globalSetup.ts"],
2122
include: ["tests/integration/**/*.test.ts"],
2223
},

0 commit comments

Comments
 (0)