From 864c6ce2b79f65947aad063a029e792af8a42d2f Mon Sep 17 00:00:00 2001 From: Mieszko Sabo <51922326+mieszkosabo@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:53:31 +0200 Subject: [PATCH] - add running tests to the CI pipeline (#12) - small refactor of shared type --- .github/workflows/checks.yml | 3 ++ bun.lockb | Bin 93450 -> 93450 bytes package.json | 1 + src/arrays.ts | 6 +-- src/object.ts | 6 +-- src/sets.ts | 6 +-- src/shared.types.ts | 6 +++ tests/arrays.test.ts | 79 +++++++++++++++++++++++++++++++++++ tests/index.test.ts | 66 +---------------------------- 9 files changed, 94 insertions(+), 79 deletions(-) create mode 100644 tests/arrays.test.ts diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 6ae2e45..44fb938 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -27,3 +27,6 @@ jobs: - name: Run check:types run: bun check:types + + - name: Run tests + run: bun test diff --git a/bun.lockb b/bun.lockb index 312a83c72b70d9b19dbd94f72a05a8bd1c6f3a7a..4150c804449ae4b82a26e45bd6b4423ee417ff94 100755 GIT binary patch delta 22 ecmeCW#oBd?b;IMo?2K{7dPaJtn_vA^$Oiy*ED7iU delta 22 acmeCW#oBd?b;IMo>`V+`u=&+rg?s>Mf(VrW diff --git a/package.json b/package.json index d2bc263..3966e8a 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "scripts": { "build": "tsup", + "clean": "rm -rf dist", "prettier:write": "prettier --write .", "check:lint": "eslint .", "check:format": "prettier --check .", diff --git a/src/arrays.ts b/src/arrays.ts index d90447e..044e91a 100644 --- a/src/arrays.ts +++ b/src/arrays.ts @@ -1,5 +1,5 @@ import { Fn } from "hotscript"; -import { Validator } from "./shared.types"; +import { Identity, Validator } from "./shared.types"; interface ArrayType extends Fn { return: this["args"] extends [...any, infer last] @@ -54,10 +54,6 @@ export const nonEmpty = () => ({ }, }); -interface Identity extends Fn { - return: this["arg0"]; -} - export const min = (min: number) => ({ name: "min" as const, $inputType: "array" as unknown as [any, ...any[]], diff --git a/src/object.ts b/src/object.ts index 7cb9be5..fa739eb 100644 --- a/src/object.ts +++ b/src/object.ts @@ -1,5 +1,5 @@ import { Fn } from "hotscript"; -import { Validator } from "./shared.types"; +import { Identity, Validator } from "./shared.types"; interface ObjectSchema extends Fn { return: MakeFieldsWithUndefinedOptional<{ @@ -52,10 +52,6 @@ type MakeFieldsWithUndefinedOptional = Expand< } & { [K in FieldsWithUndefined]?: T[K] } >; -interface Identity extends Fn { - return: this["arg0"]; -} - export const passthrough = () => { const ctx = { chain: null as Validator | null, diff --git a/src/sets.ts b/src/sets.ts index 0c48ed9..09803ae 100644 --- a/src/sets.ts +++ b/src/sets.ts @@ -1,5 +1,5 @@ import { Fn } from "hotscript"; -import { Validator } from "./shared.types"; +import { Identity, Validator } from "./shared.types"; interface SetType extends Fn { return: Set; @@ -27,10 +27,6 @@ export const set = >( }, }); -interface Identity extends Fn { - return: this["arg0"]; -} - export const nonEmpty = () => ({ name: "nonEmpty" as const, $inputType: "set" as unknown as Set, diff --git a/src/shared.types.ts b/src/shared.types.ts index 104b425..2568c4d 100644 --- a/src/shared.types.ts +++ b/src/shared.types.ts @@ -1,3 +1,5 @@ +import { Fn } from "hotscript"; + export type Validator = { name: string; $inputType: Input; @@ -17,3 +19,7 @@ export type Primitive = | symbol | null | undefined; + +export interface Identity extends Fn { + return: this["arg0"]; +} diff --git a/tests/arrays.test.ts b/tests/arrays.test.ts new file mode 100644 index 0000000..4440921 --- /dev/null +++ b/tests/arrays.test.ts @@ -0,0 +1,79 @@ +import { describe, expect, test } from "bun:test"; +import { initCorrettore, min, string, arrays, Infer } from "../src"; +import { Equal, Expect } from "./helpers.types"; + +describe("arrays", () => { + const c = initCorrettore([ + arrays.array, + string, + min, + arrays.nonEmpty, + arrays.min, + arrays.max, + arrays.length, + ]); + + test("arrays", () => { + expect(() => c.array(c.string().min(2)).parse(["ab", "ba"])).not.toThrow(); + expect(() => c.array(c.string()).parse([])).not.toThrow(); + expect(() => c.array(c.string()).parse([42])).toThrow(); + expect(() => + c.array(c.string()).nonEmpty().parse(["a", "b"]), + ).not.toThrow(); + expect(() => c.array(c.string()).nonEmpty().parse([])).toThrow(); + expect(() => + c + .array(c.string()) + .nonEmpty() + .min(3) + .max(5) + .length(4) + .parse(["a", "b", "c", "d"]), + ).not.toThrow(); + expect(() => + c + .array(c.string()) + .nonEmpty() + .min(3) + .max(5) + .length(4) + .parse(["a", "b", "d"]), + ).toThrow(); + + const schema = c.array(c.string()); + type SchemaType = Infer; + type _test = Expect>; + }); + + test("arrays alternative syntax", () => { + expect(() => c.string().min(2).array().parse(["ab", "ba"])).not.toThrow(); + expect(() => c.string().array().parse([])).not.toThrow(); + expect(() => c.string().array().parse([42])).toThrow(); + expect(() => c.string().array().nonEmpty().parse(["a", "b"])).not.toThrow(); + expect(() => c.string().array().nonEmpty().parse([])).toThrow(); + expect(() => + c + .string() + .array() + .nonEmpty() + .min(3) + .max(5) + .length(4) + .parse(["a", "b", "c", "d"]), + ).not.toThrow(); + expect(() => + c + .string() + .array() + .nonEmpty() + .min(3) + .max(5) + .length(4) + .parse(["a", "b", "d"]), + ).toThrow(); + + const schema = c.string().array(); + type SchemaType = Infer; + type _test = Expect>; + }); +}); diff --git a/tests/index.test.ts b/tests/index.test.ts index c165ac3..e017c9b 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -56,6 +56,8 @@ describe("basic tests", () => { coerce.bigintCoerce, ]); + // TODO: split into *.test.ts files and add more tests + test("smoke tests", () => { expect(() => c.number().parse(42)).not.toThrow(); expect(() => c.number().parse("hello")).toThrow(); @@ -115,70 +117,6 @@ describe("basic tests", () => { type _typeTest3 = Expect>; }); - test("arrays", () => { - expect(() => c.array(c.string().min(2)).parse(["ab", "ba"])).not.toThrow(); - expect(() => c.array(c.string()).parse([])).not.toThrow(); - expect(() => c.array(c.string()).parse([42])).toThrow(); - expect(() => - c.array(c.string()).nonEmpty().parse(["a", "b"]), - ).not.toThrow(); - expect(() => c.array(c.string()).nonEmpty().parse([])).toThrow(); - expect(() => - c - .array(c.string()) - .nonEmpty() - .min(3) - .max(5) - .length(4) - .parse(["a", "b", "c", "d"]), - ).not.toThrow(); - expect(() => - c - .array(c.string()) - .nonEmpty() - .min(3) - .max(5) - .length(4) - .parse(["a", "b", "d"]), - ).toThrow(); - - const schema = c.array(c.string()); - type SchemaType = Infer; - type _test = Expect>; - }); - - test("arrays alternative syntax", () => { - expect(() => c.string().min(2).array().parse(["ab", "ba"])).not.toThrow(); - expect(() => c.string().array().parse([])).not.toThrow(); - expect(() => c.string().array().parse([42])).toThrow(); - expect(() => c.string().array().nonEmpty().parse(["a", "b"])).not.toThrow(); - expect(() => c.string().array().nonEmpty().parse([])).toThrow(); - expect(() => - c - .string() - .array() - .nonEmpty() - .min(3) - .max(5) - .length(4) - .parse(["a", "b", "c", "d"]), - ).not.toThrow(); - expect(() => - c - .string() - .array() - .nonEmpty() - .min(3) - .max(5) - .length(4) - .parse(["a", "b", "d"]), - ).toThrow(); - - const schema = c.string().array(); - type SchemaType = Infer; - type _test = Expect>; - }); - test("sets", () => { expect(() => c.set(c.string().min(2)).parse(new Set(["ab", "ba"])),