Skip to content

Commit

Permalink
added unit tests for result
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienR1 committed Nov 20, 2023
1 parent ec3f990 commit c37ffb6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/result.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, test } from "bun:test";
import { Err, Ok } from "./result.js";

describe("result", () => {
test("Err", () => {
const result = Err(new Error("test error"));
expect(result.success).toBeFalse();
if (!result.success) {
expect(result.error.message).toBe("test error");
}

// @ts-expect-error
result.payload;
});

test("Err different type", () => {
const result = Err<string>("test error");
expect(result.success).toBeFalse();
if (!result.success) {
expect(result.error).toBe("test error");
}
});

test("Ok", () => {
const result = Ok();
expect(result.success).toBeTrue();
expect("payload" in result).toBeFalse();

// @ts-expect-error
result.payload;
// @ts-expect-error
result.error;
});

test("Ok payload", () => {
const result = Ok("test payload");
expect(result.success).toBeTrue();
if (result.success) {
expect(result.payload).toBe("test payload");
}

// @ts-expect-error
result.error;
});
});
2 changes: 1 addition & 1 deletion src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function Ok<
P extends string | number | object | undefined = undefined,
R = P extends undefined ? OkResult : OkResult<P>
>(payload?: P): R {
if (payload === undefined) {
if (payload !== undefined) {
return { success: true, payload } as R;
}
return { success: true } as R;
Expand Down

0 comments on commit c37ffb6

Please sign in to comment.