-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 23 additions & 17 deletions
40
packages/effect/test/Schema/Schema/decodeUnknownSync.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
import { describe, it } from "@effect/vitest" | ||
import * as ParseResult from "effect/ParseResult" | ||
import * as S from "effect/Schema" | ||
import { Effect, ParseResult, Predicate, Schema as S } from "effect" | ||
import * as Util from "effect/test/Schema/TestUtils" | ||
import { assertIncludes, assertTrue } from "effect/test/util" | ||
import { strictEqual, throws } from "effect/test/util" | ||
|
||
const SyncEffectfulString = S.declare([], { | ||
decode: () => (u, _, ast) => | ||
Predicate.isString(u) ? Effect.succeed(u) : Effect.fail(new ParseResult.Type(ast, u, "not a string")), | ||
encode: () => (u, _, ast) => | ||
Predicate.isString(u) ? Effect.succeed(u) : Effect.fail(new ParseResult.Type(ast, u, "not a string")) | ||
}, { identifier: "SyncEffectfulString" }) | ||
|
||
describe("decodeUnknownSync", () => { | ||
it("the returned error should be a ParseError", () => { | ||
try { | ||
S.decodeUnknownSync(S.String)(1) | ||
} catch (e) { | ||
assertTrue(ParseResult.isParseError(e)) | ||
} | ||
it("should return a ParseError when the input is invalid", () => { | ||
Util.assertions.parseError(() => S.decodeUnknownSync(S.String)(1), "Expected string, actual 1") | ||
}) | ||
|
||
it("should decode synchronously even when the schema uses Effects", () => { | ||
strictEqual(S.decodeUnknownSync(SyncEffectfulString)("a"), "a") | ||
Util.assertions.parseError(() => { | ||
S.decodeUnknownSync(SyncEffectfulString)(null) | ||
}, "not a string") | ||
}) | ||
|
||
it("should throw on async", () => { | ||
it("should throw an error when the schema performs asynchronous work", () => { | ||
Util.assertions.parseError( | ||
() => S.decodeUnknownSync(Util.AsyncString)("a"), | ||
`AsyncString | ||
└─ cannot be be resolved synchronously, this is caused by using runSync on an effect that performs async work` | ||
) | ||
}) | ||
|
||
it("should throw on unexpected dependencies", () => { | ||
try { | ||
S.decodeUnknownSync(Util.DependencyString as any)("a") | ||
throw new Error("unexpected success") | ||
} catch (e: any) { | ||
assertIncludes(e.message, "Service not found: Name") | ||
} | ||
it("should throw an error when required dependencies are missing", () => { | ||
throws(() => S.decodeUnknownSync(Util.DependencyString as any)("a"), (err) => { | ||
return err instanceof ParseResult.ParseError && err.message.includes("Service not found: Name") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters