Skip to content

Commit 807d838

Browse files
authored
fix: error cause
1 parent 6a8f054 commit 807d838

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/worker/runner/test-runner/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ module.exports = class TestRunner {
130130
if (error) {
131131
filterExtraStackFrames(error);
132132

133+
let current = error.cause;
134+
let depth = 1;
135+
136+
// Propagate errors from the cause into the stack trace of the main error.
137+
while (current) {
138+
const indent = " ".repeat(depth);
139+
error.stack += `\n\n${indent}Caused by: ${current.stack.split("\n").join(`\n${indent}`)}`;
140+
141+
current = current.cause;
142+
143+
depth++;
144+
}
145+
146+
// The original cause must be removed to avoid possible duplicates later.
147+
delete error.cause;
148+
133149
await extendWithCodeSnippet(error);
134150

135151
throw Object.assign(error, results);

test/src/worker/runner/test-runner/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,19 @@ describe("worker/runner/test-runner", () => {
824824
assert.match(error.testplaneCtx, { foo: "bar" });
825825
});
826826

827+
it("should extend error with testplaneCtx object passed to execution thread", async () => {
828+
ExecutionThread.create.callsFake(() => {
829+
ExecutionThread.prototype.run.callsFake(() => {
830+
return Promise.reject(new Error("new error", { cause: new Error("original error") }));
831+
});
832+
return Object.create(ExecutionThread.prototype);
833+
});
834+
835+
const error = await run_().catch(e => e);
836+
837+
assert.include(error.stack, "Caused by: Error: original error");
838+
});
839+
827840
it("should extend testplaneCtx with empty assert view results", async () => {
828841
ExecutionThread.prototype.run.rejects(new Error());
829842

0 commit comments

Comments
 (0)