Skip to content

Commit

Permalink
fix: Improve error message on mockStep failure (#74)
Browse files Browse the repository at this point in the history
* Improve error message on mockStep failure

* Apply destructuring trick

https://stackoverflow.com/a/71850301/253468

* Fix lint

* Explicitly test error messages
  • Loading branch information
TWiStErRob authored Feb 13, 2024
1 parent 0b6f849 commit a096aab
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/step-mocker/step-mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class StepMocker {
this.updateStep(workflow, jobId, stepIndex, mockStep);
}
} else {
throw new Error("Could not find step");
const { mockWith: _, ...stepQuery } = mockStep;
throw new Error(`Could not find step ${JSON.stringify(stepQuery)} in job ${jobId}\nin ${filePath}`);
}
}
stepsToAdd.forEach(s => this.addStep(workflow, s.jobId, s.stepIndex, s.mockStep));
Expand Down
94 changes: 90 additions & 4 deletions test/unit/step-mocker/step-mocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ describe("locateSteps", () => {
existsSyncMock.mockReturnValueOnce(true);
writeFileSyncMock.mockReturnValueOnce(undefined);
});
test("no job found", async () => {

test("no job found using wrong name", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
Expand All @@ -117,7 +118,7 @@ describe("locateSteps", () => {
},
],
})
).rejects.toThrowError();
).rejects.toThrowError(`Could not find step {"name":"step"} in job incorrectName\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using id", async () => {
Expand All @@ -142,6 +143,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect id", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
id: "not-echo",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"id":"not-echo"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using name", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -162,6 +180,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect name", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
name: "Incorrect Name",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"name":"Incorrect Name"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using uses", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -184,6 +219,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect uses", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
uses: "invalid/action@v0",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"uses":"invalid/action@v0"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using run", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -203,7 +255,24 @@ describe("locateSteps", () => {
"utf8"
);
});


test("no step found using incorrect run", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
run: "echo incorrect",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"run":"echo incorrect"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using index", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -224,6 +293,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect index", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
index: 42,
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"index":42} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test.each([
["index", 0, 0],
["name", "secrets", 0]
Expand Down Expand Up @@ -469,4 +555,4 @@ describe("add step", () => {
"utf8"
);
});
});
});

0 comments on commit a096aab

Please sign in to comment.