Skip to content

Commit

Permalink
#3171: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Apr 14, 2022
1 parent b45d63f commit 96c41af
Showing 1 changed file with 76 additions and 11 deletions.
87 changes: 76 additions & 11 deletions src/runtime/pipelineTests/trace.test.ts
Expand Up @@ -60,20 +60,71 @@ function makeExpression(template: TemplateEngine, value: string): Expression {
};
}

describe("Trace exceptional exit", () => {
describe("Trace normal exit", () => {
test("trace entry and normal exit", async () => {
const instanceId = uuidv4();

const result = await reducePipeline(
{
id: echoBlock.id,
config: { message: "{{@input.inputArg}}" },
config: { message: makeExpression("nunjucks", "{{@input.inputArg}}") },
instanceId,
},
simpleInput({ inputArg: "hello" }),
testOptions("v3")
);
expect(result).toStrictEqual({ message: "{{@input.inputArg}}" });

expect(result).toStrictEqual({ message: "hello" });

expect(traces.addEntry).toHaveBeenCalledTimes(1);
expect(traces.addEntry).toHaveBeenCalledWith(
expect.objectContaining({
renderedArgs: expect.objectContaining({
message: "hello",
}),
renderError: null,
})
);
expect(traces.addExit).toHaveBeenCalledTimes(1);
});
});

describe("Trace render error", () => {
test("Trace input render error", async () => {
const instanceId = uuidv4();

try {
await reducePipeline(
{
id: echoBlock.id,
config: { message: makeExpression("var", "@doesNotExist.bar") },
instanceId,
},
simpleInput({ inputArg: "hello" }),
testOptions("v3")
);
} catch {
// Error is expected
}

expect(traces.addEntry).toHaveBeenCalledTimes(1);
expect(traces.addEntry).toHaveBeenCalledWith(
expect.objectContaining({
renderedArgs: undefined,
renderError: expect.objectContaining({
message: expect.anything(),
}),
})
);

expect(traces.addExit).toHaveBeenCalledTimes(1);
expect(traces.addExit).toHaveBeenCalledWith(
expect.objectContaining({
error: expect.objectContaining({
message: "@doesNotExist.bar undefined (missing @doesNotExist)",
}),
})
);
});
});

Expand Down Expand Up @@ -102,7 +153,19 @@ describe("Trace conditional execution", () => {
);

expect(traces.addEntry).toHaveBeenCalledTimes(2);

expect(traces.addEntry).toHaveBeenCalledWith(
expect.objectContaining({
renderedArgs: { message: "hello" },
})
);

expect(traces.addExit).toHaveBeenCalledTimes(2);
expect(traces.addExit).toHaveBeenCalledWith(
expect.objectContaining({
skippedRun: true,
})
);
});
});

Expand Down Expand Up @@ -249,13 +312,15 @@ describe("Trace normal execution", () => {
};

expect(traces.addExit).toHaveBeenCalledTimes(1);

// Can't use toHaveBeenNthCalledWith because we don't want to include the stack trace in the test
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- test code
const args = (traces.addExit as any).mock.calls[0][0];
expect(args.runId).toBe(meta.runId);
expect(args.blockInstanceId).toBe(meta.blockInstanceId);
expect(args.error.name).toBe("BusinessError");
expect(args.error.message).toBe("hello");
expect(traces.addExit).toHaveBeenCalledWith(
expect.objectContaining({
runId: meta.runId,
blockInstanceId: meta.blockInstanceId,
error: expect.objectContaining({
name: "BusinessError",
message: "hello",
}),
})
);
});
});

0 comments on commit 96c41af

Please sign in to comment.