test(app): tighten weak assertions in utils tests (#714)

Replace toBeDefined/toBeTruthy guards with concrete shape assertions
using toContainEqual(expect.objectContaining(...)) in diff-highlighter
tests, and toMatch(/^script-draft-\d+$/) for the ID check in
project-config-form tests.
This commit is contained in:
Mohamed Boudra
2026-05-05 19:58:55 +07:00
parent 6bd5d4c410
commit 0a84c613f7
2 changed files with 43 additions and 45 deletions
+42 -44
View File
@@ -285,18 +285,12 @@ describe("highlightDiffFile", () => {
// First content line: "const foo = 1;" // First content line: "const foo = 1;"
const constLine = hunk.lines[1]; const constLine = hunk.lines[1];
expect(constLine.tokens).toBeDefined(); expect(constLine.tokens).toContainEqual(
expect(constLine.tokens!.length).toBeGreaterThan(0); expect.objectContaining({ text: "const", style: "keyword" }),
);
// Should have a "keyword" token for "const" expect(constLine.tokens).toContainEqual(
const constToken = constLine.tokens!.find((t) => t.text === "const"); expect.objectContaining({ text: "1", style: "number" }),
expect(constToken).toBeDefined(); );
expect(constToken!.style).toBe("keyword");
// Should have a "number" token for "1"
const numberToken = constLine.tokens!.find((t) => t.text === "1");
expect(numberToken).toBeDefined();
expect(numberToken!.style).toBe("number");
}); });
it("highlights both added and removed lines correctly", () => { it("highlights both added and removed lines correctly", () => {
@@ -306,15 +300,15 @@ describe("highlightDiffFile", () => {
// Removed line: "const bar = 2;" // Removed line: "const bar = 2;"
const removedLine = hunk.lines.find((l) => l.type === "remove" && l.content.includes("bar")); const removedLine = hunk.lines.find((l) => l.type === "remove" && l.content.includes("bar"));
expect(removedLine?.tokens).toBeDefined(); expect(removedLine?.tokens).toContainEqual(
const removedNumber = removedLine!.tokens!.find((t) => t.text === "2"); expect.objectContaining({ text: "2", style: "number" }),
expect(removedNumber?.style).toBe("number"); );
// Added line: "const bar = 3;" // Added line: "const bar = 3;"
const addedLine = hunk.lines.find((l) => l.type === "add" && l.content.includes("bar")); const addedLine = hunk.lines.find((l) => l.type === "add" && l.content.includes("bar"));
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
const addedNumber = addedLine!.tokens!.find((t) => t.text === "3"); expect.objectContaining({ text: "3", style: "number" }),
expect(addedNumber?.style).toBe("number"); );
}); });
it("does not modify unsupported file types", () => { it("does not modify unsupported file types", () => {
@@ -351,15 +345,16 @@ describe("highlightDiffFile", () => {
const highlighted = highlightDiffFile(files[0]); const highlighted = highlightDiffFile(files[0]);
const hunk = highlighted.hunks[0]; const hunk = highlighted.hunks[0];
const fnLine = hunk.lines[1]; expect(hunk.lines[1].tokens).toContainEqual(
expect(fnLine.tokens).toBeDefined(); expect.objectContaining({ text: "fn", style: "keyword" }),
expect(fnLine.tokens!.some((t) => t.text === "fn" && t.style === "keyword")).toBe(true); );
const addedLine = hunk.lines.find( const addedLine = hunk.lines.find(
(line) => line.type === "add" && line.content.includes("version"), (line) => line.type === "add" && line.content.includes("version"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
expect(addedLine!.tokens!.some((t) => t.text === "2" && t.style === "number")).toBe(true); expect.objectContaining({ text: "2", style: "number" }),
);
}); });
it("adds syntax highlighting tokens to C code", () => { it("adds syntax highlighting tokens to C code", () => {
@@ -367,15 +362,14 @@ describe("highlightDiffFile", () => {
const highlighted = highlightDiffFile(files[0]); const highlighted = highlightDiffFile(files[0]);
const hunk = highlighted.hunks[0]; const hunk = highlighted.hunks[0];
const mainLine = hunk.lines[1]; expect(hunk.lines[1].tokens?.length).toBeGreaterThan(0);
expect(mainLine.tokens).toBeDefined();
expect(mainLine.tokens!.length).toBeGreaterThan(0);
const addedLine = hunk.lines.find( const addedLine = hunk.lines.find(
(line) => line.type === "add" && line.content.includes("version"), (line) => line.type === "add" && line.content.includes("version"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
expect(addedLine!.tokens!.some((t) => t.text === "2" && t.style === "number")).toBe(true); expect.objectContaining({ text: "2", style: "number" }),
);
}); });
it("adds syntax highlighting tokens to Java code", () => { it("adds syntax highlighting tokens to Java code", () => {
@@ -383,15 +377,16 @@ describe("highlightDiffFile", () => {
const highlighted = highlightDiffFile(files[0]); const highlighted = highlightDiffFile(files[0]);
const hunk = highlighted.hunks[0]; const hunk = highlighted.hunks[0];
const classLine = hunk.lines[1]; expect(hunk.lines[1].tokens).toContainEqual(
expect(classLine.tokens).toBeDefined(); expect.objectContaining({ text: "public", style: "keyword" }),
expect(classLine.tokens!.some((t) => t.text === "public" && t.style === "keyword")).toBe(true); );
const addedLine = hunk.lines.find( const addedLine = hunk.lines.find(
(line) => line.type === "add" && line.content.includes("version"), (line) => line.type === "add" && line.content.includes("version"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
expect(addedLine!.tokens!.some((t) => t.text === "2" && t.style === "number")).toBe(true); expect.objectContaining({ text: "2", style: "number" }),
);
}); });
it("adds syntax highlighting tokens to Objective-C file extensions", () => { it("adds syntax highlighting tokens to Objective-C file extensions", () => {
@@ -402,8 +397,9 @@ describe("highlightDiffFile", () => {
const addedLine = hunk.lines.find( const addedLine = hunk.lines.find(
(line) => line.type === "add" && line.content.includes("version"), (line) => line.type === "add" && line.content.includes("version"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
expect(addedLine!.tokens!.some((t) => t.text === "2" && t.style === "number")).toBe(true); expect.objectContaining({ text: "2", style: "number" }),
);
}); });
it("adds syntax highlighting tokens to Go code", () => { it("adds syntax highlighting tokens to Go code", () => {
@@ -413,8 +409,9 @@ describe("highlightDiffFile", () => {
(line) => line.type === "add" && line.content.includes("version"), (line) => line.type === "add" && line.content.includes("version"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
expect(addedLine!.tokens!.some((t) => t.text === "2" && t.style === "number")).toBe(true); expect.objectContaining({ text: "2", style: "number" }),
);
}); });
it("adds syntax highlighting tokens to PHP code", () => { it("adds syntax highlighting tokens to PHP code", () => {
@@ -424,8 +421,9 @@ describe("highlightDiffFile", () => {
(line) => line.type === "add" && line.content.includes("$version"), (line) => line.type === "add" && line.content.includes("$version"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(
expect(addedLine!.tokens!.some((t) => t.text === "2" && t.style === "number")).toBe(true); expect.objectContaining({ text: "2", style: "number" }),
);
}); });
it("adds syntax highlighting tokens to YAML code", () => { it("adds syntax highlighting tokens to YAML code", () => {
@@ -435,8 +433,7 @@ describe("highlightDiffFile", () => {
(line) => line.type === "add" && line.content.includes("count"), (line) => line.type === "add" && line.content.includes("count"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens?.length).toBeGreaterThan(0);
expect(addedLine!.tokens!.length).toBeGreaterThan(0);
}); });
it("adds syntax highlighting tokens to XML code", () => { it("adds syntax highlighting tokens to XML code", () => {
@@ -446,8 +443,7 @@ describe("highlightDiffFile", () => {
(line) => line.type === "add" && line.content.includes("<count>"), (line) => line.type === "add" && line.content.includes("<count>"),
); );
expect(addedLine?.tokens).toBeDefined(); expect(addedLine?.tokens).toContainEqual(expect.objectContaining({ style: "tag" }));
expect(addedLine!.tokens!.some((t) => t.style === "tag")).toBe(true);
}); });
}); });
@@ -456,7 +452,9 @@ describe("parseAndHighlightDiff", () => {
const files = parseAndHighlightDiff(SIMPLE_DIFF); const files = parseAndHighlightDiff(SIMPLE_DIFF);
expect(files).toHaveLength(1); expect(files).toHaveLength(1);
expect(files[0].hunks[0].lines[1].tokens).toBeDefined(); expect(files[0].hunks[0].lines[1].tokens).toContainEqual(
expect.objectContaining({ text: "const", style: "keyword" }),
);
}); });
it("handles multiple files", () => { it("handles multiple files", () => {
@@ -49,7 +49,7 @@ describe("configToDraft", () => {
expect(devRow.commandOriginalKind).toBe("string"); expect(devRow.commandOriginalKind).toBe("string");
expect(devRow.type).toBe("long-running"); expect(devRow.type).toBe("long-running");
expect(devRow.portText).toBe("3000"); expect(devRow.portText).toBe("3000");
expect(devRow.id).toBeTruthy(); expect(devRow.id).toMatch(/^script-draft-\d+$/);
expect(buildRow.name).toBe("build"); expect(buildRow.name).toBe("build");
expect(buildRow.commandText).toBe("npm\nrun\nbuild"); expect(buildRow.commandText).toBe("npm\nrun\nbuild");
expect(buildRow.commandOriginalKind).toBe("array"); expect(buildRow.commandOriginalKind).toBe("array");