Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for Node/Bun #1

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/test-node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test (Node)

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22.x
- name: Install deps
run: |
npx jsr install
- name: Test
run: |
npx --yes tsx --test *_test.ts
timeout-minutes: 5
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/npm
deno.lock
.coverage
node_modules
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
]
},
"imports": {
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.0",
"@core/errorutil": "./mod.ts",
"@core/errorutil/error-object": "./error_object.ts",
"@core/errorutil/raise": "./raise.ts",
"@core/errorutil/try-or": "./try_or.ts",
"@core/errorutil/try-or-else": "./try_or_else.ts",
"@core/errorutil/unimplemented": "./unimplemented.ts",
"@core/errorutil/unreachable": "./unreachable.ts",
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.0",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@std/assert": "jsr:@std/assert@^1.0.0"
},
"tasks": {
Expand Down
91 changes: 44 additions & 47 deletions error_object_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { test } from "@cross/test";
import { assertEquals, assertInstanceOf } from "@std/assert";
import { fromErrorObject, toErrorObject } from "./error_object.ts";

Expand All @@ -12,57 +13,53 @@ class CustomError extends Error {
}
}

Deno.test("toErrorObject", async (t) => {
await t.step("Error", () => {
const err = new Error("error");
err.stack = "stack...";
const obj = toErrorObject(err);
assertEquals(obj, {
proto: "Error",
name: "Error",
message: "error",
stack: "stack...",
attributes: {},
});
await test("toErrorObject with Error", () => {
const err = new Error("error");
err.stack = "stack...";
const obj = toErrorObject(err);
assertEquals(obj, {
proto: "Error",
name: "Error",
message: "error",
stack: "stack...",
attributes: {},
});
});

await t.step("CustomError", () => {
const err = new CustomError("error", "foo", 10);
err.stack = "stack...";
const obj = toErrorObject(err);
assertEquals(obj, {
proto: "CustomError",
name: "ThisIsCustomError",
message: "error",
stack: "stack...",
attributes: {
foo: "foo",
bar: 10,
},
});
await test("toErrorObject with CustomError", () => {
const err = new CustomError("error", "foo", 10);
err.stack = "stack...";
const obj = toErrorObject(err);
assertEquals(obj, {
proto: "CustomError",
name: "ThisIsCustomError",
message: "error",
stack: "stack...",
attributes: {
foo: "foo",
bar: 10,
},
});
});

Deno.test("fromErrorObject", async (t) => {
await t.step("Error", () => {
const obj = toErrorObject(new Error("error"));
obj.stack = "stack...";
const err = fromErrorObject(obj);
assertInstanceOf(err, Error);
assertEquals(err.name, "Error");
assertEquals(err.message, "error");
assertEquals(err.stack, "stack...");
});
await test("fromErrorObject with Error", () => {
const obj = toErrorObject(new Error("error"));
obj.stack = "stack...";
const err = fromErrorObject(obj);
assertInstanceOf(err, Error);
assertEquals(err.name, "Error");
assertEquals(err.message, "error");
assertEquals(err.stack, "stack...");
});

await t.step("CustomError", () => {
const obj = toErrorObject(new CustomError("error", "foo", 10));
obj.stack = "stack...";
const err = fromErrorObject(obj);
assertInstanceOf(err, Error);
assertEquals(err.name, "ThisIsCustomError");
assertEquals(err.message, "error");
assertEquals(err.stack, "stack...");
assertEquals((err as CustomError).foo, "foo");
assertEquals((err as CustomError).bar, 10);
});
await test("fromErrorObject with CustomError", () => {
const obj = toErrorObject(new CustomError("error", "foo", 10));
obj.stack = "stack...";
const err = fromErrorObject(obj);
assertInstanceOf(err, Error);
assertEquals(err.name, "ThisIsCustomError");
assertEquals(err.message, "error");
assertEquals(err.stack, "stack...");
assertEquals((err as CustomError).foo, "foo");
assertEquals((err as CustomError).bar, 10);
});
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "module",
"dependencies": {
"@core/unknownutil": "npm:@jsr/core__unknownutil@^4.2.0",
"@cross/test": "npm:@jsr/cross__test@^0.0.9",
"@std/assert": "npm:@jsr/std__assert@^1.0.2"
}
}
3 changes: 2 additions & 1 deletion raise_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test } from "@cross/test";
import { assertThrows } from "@std/assert";
import { raise } from "./raise.ts";

Deno.test("raise", () => {
test("raise", () => {
assertThrows(() => raise(new Error("error")), Error, "error");
});
97 changes: 48 additions & 49 deletions try_or_else_test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
import { test } from "@cross/test";
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { raise } from "./raise.ts";
import { tryOrElse } from "./try_or_else.ts";

Deno.test("tryOrElse", async (t) => {
const err = new Error("error");
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);
const err = new Error("error");
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);

await t.step("sync", () => {
type T = number;
assertEquals(tryOrElse((): T => 1, () => 2), 1);
assertEquals(tryOrElse((): T => raise(err), () => 2), 2);
});
await test("tryOrElse (sync)", () => {
type T = number;
assertEquals(tryOrElse((): T => 1, () => 2), 1);
assertEquals(tryOrElse((): T => raise(err), () => 2), 2);
});

await t.step("sync (error)", () => {
type T = number;
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});
await test("tryOrElse with Error (sync)", () => {
type T = number;
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});

await t.step("async", async () => {
type T = Promise<number>;
assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1);
assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1);
assertEquals(await tryOrElse((): T => reject(err), () => 2), 2);
assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2);
assertEquals(await tryOrElse((): T => raise(err), () => 2), 2);
assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2);
});
await test("tryOrElse (async)", async () => {
type T = Promise<number>;
assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1);
assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1);
assertEquals(await tryOrElse((): T => reject(err), () => 2), 2);
assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2);
assertEquals(await tryOrElse((): T => raise(err), () => 2), 2);
assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2);
});

await t.step("async (error)", async () => {
type T = Promise<number>;
await assertRejects(
() => tryOrElse((): T => reject(err), () => reject(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => reject(err), () => raise(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => raise(err), () => reject(err)),
Error,
"error",
);
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});
await test("tryOrElse with Error (async)", async () => {
type T = Promise<number>;
await assertRejects(
() => tryOrElse((): T => reject(err), () => reject(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => reject(err), () => raise(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => raise(err), () => reject(err)),
Error,
"error",
);
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});
35 changes: 17 additions & 18 deletions try_or_test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { raise } from "./raise.ts";
import { tryOr } from "./try_or.ts";

Deno.test("tryOr", async (t) => {
const err = new Error("error");
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);
const err = new Error("error");
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);

await t.step("sync", () => {
type T = number;
assertEquals(tryOr((): T => 1, 2), 1);
assertEquals(tryOr((): T => raise(err), 2), 2);
});
await test("tryOr (sync)", () => {
type T = number;
assertEquals(tryOr((): T => 1, 2), 1);
assertEquals(tryOr((): T => raise(err), 2), 2);
});

await t.step("async", async () => {
type T = Promise<number>;
assertEquals(await tryOr((): T => resolve(1), 2), 1);
assertEquals(await tryOr((): T => resolve(1), resolve(2)), 1);
assertEquals(await tryOr((): T => reject(err), 2), 2);
assertEquals(await tryOr((): T => reject(err), resolve(2)), 2);
assertEquals(await tryOr((): T => raise(err), 2), 2);
assertEquals(await tryOr((): T => raise(err), resolve(2)), 2);
});
await test("tryOr (async)", async () => {
type T = Promise<number>;
assertEquals(await tryOr((): T => resolve(1), 2), 1);
assertEquals(await tryOr((): T => resolve(1), resolve(2)), 1);
assertEquals(await tryOr((): T => reject(err), 2), 2);
assertEquals(await tryOr((): T => reject(err), resolve(2)), 2);
assertEquals(await tryOr((): T => raise(err), 2), 2);
assertEquals(await tryOr((): T => raise(err), resolve(2)), 2);
});
7 changes: 7 additions & 0 deletions unimplemented_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "@cross/test";
import { assertThrows } from "@std/assert";
import { unimplemented, UnimplementedError } from "./unimplemented.ts";

test("unimplemented", () => {
assertThrows(() => unimplemented(), UnimplementedError);
});
7 changes: 7 additions & 0 deletions unreachable_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "@cross/test";
import { assertThrows } from "@std/assert";
import { unreachable, UnreachableError } from "./unreachable.ts";

test("unreachable", () => {
assertThrows(() => unreachable(), UnreachableError);
});
Loading