Skip to content

Commit

Permalink
fix: improve error message in sdk custom matchers (#1417)
Browse files Browse the repository at this point in the history
* fix: improve error message in sdk custom matchers

* fix: typo

Co-authored-by: Micaiah Reid <[email protected]>

---------

Co-authored-by: Micaiah Reid <[email protected]>
  • Loading branch information
hugocaillard and MicaiahReid authored Apr 12, 2024
1 parent 397d8a4 commit a7bd073
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,17 @@ function checkCVType<T extends ClarityType>(
const isCVWithType = isClarityValueWithType(actual, expectedType);

if (!isCVWithType) {
// for readability, the error diff is kept short if the developers uses the wrong `expect<ClarityType>`
// ideally, we should have a way to display short message diffs even if the actual and/or expected data are big lists/tuples/buffers

// for now, we make an exception and display the full error message if the actual value is a ResponseErr
const errorCode = actual.type === ClarityType.ResponseErr ? ` ${Cl.prettyPrint(actual)}` : "";

throw new ClarityTypeError({
// generic and short message
message: `actual value must ${notStr(isNot)}be a Clarity "${
ClarityType[expectedType]
}", received "${ClarityType[actual.type]}"`,
}", received "${ClarityType[actual.type]}"${errorCode}`,
actual: ClarityType[actual.type],
expected: ClarityType[expectedType],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ describe("test nested types", () => {
});
});

it("display values on one line in message", () => {
it("displays values on one line in message", () => {
const failingTest = () =>
expect(Cl.ok(Cl.tuple({ counter: Cl.uint(1) }))).toBeOk(Cl.tuple({ counter: Cl.uint(2) }));

Expand All @@ -436,4 +436,20 @@ describe("test nested types", () => {
expect(e.expected).toBe("(ok {\n counter: u2\n})");
}
});

it("displays short message of the matchers does not match the actual clarity type", () => {
const value = Cl.ok(Cl.uint(1));
const failingTest = () => expect(value).toBeList([]);

expect(failingTest).toThrow('actual value must be a Clarity "List", received "ResponseOk"');
});

it("displays error code even if `err` is not the expected type", () => {
const value = Cl.error(Cl.uint(1));
const failingTest = () => expect(value).toBeOk(Cl.tuple({ counter: Cl.uint(2) }));

expect(failingTest).toThrow(
'actual value must be a Clarity "ResponseOk", received "ResponseErr" (err u1)',
);
});
});

0 comments on commit a7bd073

Please sign in to comment.