Skip to content

Commit

Permalink
Share the run function for better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Oct 27, 2023
1 parent d48c8ea commit 05f1c4f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 88 deletions.
20 changes: 8 additions & 12 deletions examples/extra-fields.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from "vitest";

import { Decoder, fieldsAuto, format, map, number, string } from "..";
import { Decoder, fieldsAuto, map, number, string } from "..";
import { run } from "../tests/helpers";

test("adding extra fields to records", () => {
// Want to add an extra field to a record, that doesn’t look at the input at
Expand Down Expand Up @@ -43,17 +44,12 @@ test("adding extra fields to records", () => {
});

// It no longer works, because all the fields you mentioned are expected to exist.
const decoderResult = productDecoderBroken(data);
expect(
decoderResult.tag === "DecoderError"
? format(decoderResult.error)
: decoderResult,
).toMatchInlineSnapshot(`
"At root:
Expected an object with a field called: \\"version\\"
expect(run(productDecoderBroken, data)).toMatchInlineSnapshot(`
At root:
Expected an object with a field called: "version"
Got: {
\\"name\\": \\"Comfortable Bed\\",
\\"price\\": 10000
}"
"name": "Comfortable Bed",
"price": 10000
}
`);
});
47 changes: 15 additions & 32 deletions examples/fieldsUnion-fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expectType, TypeEqual } from "ts-expect";
import { expect, test } from "vitest";

import { Decoder, fieldsUnion, Infer, number, tag } from "../";
import { run } from "../tests/helpers";

test("fieldsUnion with fallback for unknown tags", () => {
// Here’s a helper function that takes a decoder – which is supposed to be a
Expand Down Expand Up @@ -65,44 +66,26 @@ test("fieldsUnion with fallback for unknown tags", () => {
});

// The original decoder fails on unknown tags, while the other one returns `undefined`.
expect(decoder({ tag: "Three" })).toMatchInlineSnapshot(`
{
"error": {
"got": "Three",
"knownTags": [
"One",
"Two",
],
"path": [
"tag",
],
"tag": "unknown fieldsUnion tag",
},
"tag": "DecoderError",
}
expect(run(decoder, { tag: "Three" })).toMatchInlineSnapshot(`
At root["tag"]:
Expected one of these tags:
"One",
"Two"
Got: "Three"
`);
expect(decoderWithFallback({ tag: "Three" })).toStrictEqual({
tag: "Valid",
value: undefined,
});

// A nested `fieldsUnion` still fails on unknown tags:
expect(decoderWithFallback({ tag: "Two", value: { tag: "Rectangle" } }))
.toMatchInlineSnapshot(`
{
"error": {
"got": "Rectangle",
"knownTags": [
"Circle",
"Square",
],
"path": [
"value",
"tag",
],
"tag": "unknown fieldsUnion tag",
},
"tag": "DecoderError",
}
expect(
run(decoderWithFallback, { tag: "Two", value: { tag: "Rectangle" } }),
).toMatchInlineSnapshot(`
At root["value"]["tag"]:
Expected one of these tags:
"Circle",
"Square"
Got: "Rectangle"
`);
});
24 changes: 2 additions & 22 deletions examples/readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,13 @@ import {
DecoderResult,
field,
fieldsAuto,
format,
Infer,
number,
repr,
ReprOptions,
string,
undefinedOr,
} from "..";

function run<T>(
decoder: Decoder<T>,
value: unknown,
options?: ReprOptions,
): T | string {
const decoderResult = decoder(value);
switch (decoderResult.tag) {
case "DecoderError":
return format(decoderResult.error, options);
case "Valid":
return decoderResult.value;
}
}

expect.addSnapshotSerializer({
test: (value: unknown): boolean => typeof value === "string",
print: String,
});
import { run } from "../tests/helpers";

test("the main readme example", () => {
type User = {
Expand Down Expand Up @@ -117,7 +97,7 @@ test("default vs sensitive error messages", () => {
error instanceof Error ? error.message : `Unknown error: ${repr(error)}`;
}

expect(message).toMatchInlineSnapshot("Expected userDecoder to fail!");
expect(message).toMatchInlineSnapshot('"Expected userDecoder to fail!"');

expect(run(userDecoder, data)).toMatchInlineSnapshot(`
At root["details"]["ssn"]:
Expand Down
23 changes: 1 addition & 22 deletions tests/decoders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,20 @@ import {
fieldsAuto,
fieldsUnion,
flatMap,
format,
Infer,
map,
multi,
nullable,
number,
record,
recursive,
ReprOptions,
string,
stringUnion,
tag,
tuple,
undefinedOr,
} from "..";

function run<T>(
decoder: Decoder<T>,
value: unknown,
options?: ReprOptions,
): T | string {
const decoderResult = decoder(value);
switch (decoderResult.tag) {
case "DecoderError":
return format(decoderResult.error, options);
case "Valid":
return decoderResult.value;
}
}

expect.addSnapshotSerializer({
test: (value: unknown): boolean =>
typeof value === "string" && value.includes("At root"),
print: String,
});
import { run } from "./helpers";

test("boolean", () => {
expect(boolean(true)).toStrictEqual({ tag: "Valid", value: true });
Expand Down
23 changes: 23 additions & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from "vitest";

import { Decoder, format, ReprOptions } from "..";

export function run<T>(
decoder: Decoder<T>,
value: unknown,
options?: ReprOptions,
): T | string {
const decoderResult = decoder(value);
switch (decoderResult.tag) {
case "DecoderError":
return format(decoderResult.error, options);
case "Valid":
return decoderResult.value;
}
}

expect.addSnapshotSerializer({
test: (value: unknown): boolean =>
typeof value === "string" && value.includes("At root"),
print: String,
});

0 comments on commit 05f1c4f

Please sign in to comment.