Skip to content

Commit

Permalink
fix undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Jan 13, 2025
1 parent 46fc090 commit 4255863
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/allure-js-commons/src/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export const stripAnsi = (str: string): string => {

export const getMessageAndTraceFromError = (error: Error | { message?: string; stack?: string }): StatusDetails => {
const { message, stack } = error;
const actual = "actual" in error ? serialize(error.actual) : undefined;
const expected = "expected" in error ? serialize(error.expected) : undefined;
const actual = "actual" in error && error.actual !== undefined ? { actual: serialize(error.actual) } : {};
const expected = "expected" in error && error.expected !== undefined ? { expected: serialize(error.expected) } : {};
return {
message: message ? stripAnsi(message) : undefined,
trace: stack ? stripAnsi(stack) : undefined,
actual,
expected,
...actual,
...expected,
};
};

Expand Down
14 changes: 14 additions & 0 deletions packages/allure-js-commons/test/sdk/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@ describe("getMessageAndTraceFromError", () => {
});
});

it("should ignore undefined actual value", () => {
const error: Error & { actual?: string } = new Error("some message");
error.actual = undefined;
const result = getMessageAndTraceFromError(error);
expect(result).not.toHaveProperty("actual");
});

it("should return expected from error", () => {
const error: Error & { expected?: string } = new Error("some message");
error.expected = "some expected value";
Expand All @@ -539,4 +546,11 @@ describe("getMessageAndTraceFromError", () => {
expected: "some expected value",
});
});

it("should ignore undefined expected value", () => {
const error: Error & { expected?: string } = new Error("some message");
error.expected = undefined;
const result = getMessageAndTraceFromError(error);
expect(result).not.toHaveProperty("expected");
});
});
24 changes: 24 additions & 0 deletions packages/allure-vitest/test/spec/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,28 @@ describe("expect", () => {
},
]);
});

// this is the way vitest process errors
it("should add actual and expected values when regular exception is thrown", async () => {
const { tests } = await runVitestInlineTest(`
import { test, expect } from "vitest";
test("fail test", () => {
throw new Error("fail!")
});
`);

expect(tests).toHaveLength(1);
expect(tests).toMatchObject([
{
name: "fail test",
status: "broken",
statusDetails: {
expected: "undefined",
actual: "undefined",
},
},
]);
});
});

0 comments on commit 4255863

Please sign in to comment.