From c7c53f533ceadd3bb955841c3068a14850cc7ba5 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:00:21 +0200 Subject: [PATCH 1/4] chore: add test:typecheck for checking types in tests --- package.json | 1 + tsconfig.test.json | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 tsconfig.test.json diff --git a/package.json b/package.json index a98fb08..6577984 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "format": "prettier '**/*' -u -w", "prepack": "yarn test && yarn build", "test": "vitest run", + "test:typecheck": "tsc --noEmit -p tsconfig.test.json", "typecheck": "tsc --noEmit", "benchmark": "node benchmark/src/option && node benchmark/src/result && node benchmark/src/future && node benchmark/src/future-result" }, diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000..2b1ab51 --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "include": ["**/*.test.ts"], + "exclude": ["node_modules", "dist"], + "compilerOptions": { + "noEmit": true, + "skipLibCheck": true + }, +} From d43808a44cdb21591f5dc6bab2e2cf605199a394 Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:05:38 +0200 Subject: [PATCH 2/4] fix(test): incorrect typing of Future --- test/Future.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Future.test.ts b/test/Future.test.ts index b138f25..e79b109 100644 --- a/test/Future.test.ts +++ b/test/Future.test.ts @@ -464,7 +464,7 @@ test("Future concurrent", async () => { --parallel; }), () => - Future.make<4>((resolve) => { + Future.make((resolve) => { expect(++parallel).toBeLessThanOrEqual(2); setTimeout(() => resolve(undefined), 75); }) From 7782abb1cbb22aef822f01a7a0fa068b1f22d6dc Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:10:17 +0200 Subject: [PATCH 3/4] test: add tests for to/from JSON --- test/AsyncData.test.ts | 9 +++++++++ test/Option.test.ts | 9 +++++++++ test/Result.test.ts | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/test/AsyncData.test.ts b/test/AsyncData.test.ts index 574ec69..0426746 100644 --- a/test/AsyncData.test.ts +++ b/test/AsyncData.test.ts @@ -380,3 +380,12 @@ test("AsyncData isAsyncData", async () => { expect(AsyncData.isAsyncData([])).toEqual(false); expect(AsyncData.isAsyncData({})).toEqual(false); }); + +test("AsyncData JSON serialization", () => { + const data = AsyncData.Loading(); + expect( + AsyncData.fromJSON(data.toJSON()).tap(() => { + // use async data + }), + ).toEqual(data); +}); diff --git a/test/Option.test.ts b/test/Option.test.ts index 5e65df1..14488cf 100644 --- a/test/Option.test.ts +++ b/test/Option.test.ts @@ -202,3 +202,12 @@ test("Option.isOption", async () => { expect(Option.isOption([])).toEqual(false); expect(Option.isOption({})).toEqual(false); }); + +test("Option JSON serialization", () => { + const option = Option.None(); + expect( + Option.fromJSON(option.toJSON()).tap(() => { + // Use option + }), + ).toEqual(option); +}); diff --git a/test/Result.test.ts b/test/Result.test.ts index 3bb3f93..90b4f80 100644 --- a/test/Result.test.ts +++ b/test/Result.test.ts @@ -253,3 +253,19 @@ test("Result.isResult", async () => { expect(Result.isResult([])).toEqual(false); expect(Result.isResult({})).toEqual(false); }); + +test("Result JSON serialization", async () => { + const resultOk = Result.Ok({ thing: "hello" }); + expect( + Result.fromJSON(resultOk.toJSON()).tap(() => { + // Use result + }), + ).toEqual(resultOk); + + const resultError = Result.Error(new Error("Oops")); + expect( + Result.fromJSON(resultError.toJSON()).tap(() => { + // Use result + }), + ).toEqual(resultError); +}); From ac7c834c30c3397403e977917e65ae9b9f917aaf Mon Sep 17 00:00:00 2001 From: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:16:54 +0200 Subject: [PATCH 4/4] fix: specify result type for Result.fromJSON --- src/OptionResult.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OptionResult.ts b/src/OptionResult.ts index d1c910d..8ed321b 100644 --- a/src/OptionResult.ts +++ b/src/OptionResult.ts @@ -421,7 +421,7 @@ class __Result { return false; }; - static fromJSON = (value: JsonResult) => { + static fromJSON = (value: JsonResult): Result => { return value.tag === "Ok" ? Result.Ok(value.value) : Result.Error(value.error);