This repository has been archived on 2026-03-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

50 lines
1.2 KiB
TypeScript

import * as t from "..";
test("converts to typescript", () => {
expect(
t.toTypescript(t.num.validate("greater than zero", (num) => num > 0))
).toEqual("// greater than zero\nnumber");
});
test("converts to JSON schema as a comment when errorOnValidations is false", () => {
expect(
t.toJSONSchema(">0", t.num.validate("greater than zero", (num) => num > 0), {
errorOnValidations: false,
})
).toEqual({
$schema: t.JSON_SCHEMA_VERSION,
title: ">0",
description: "greater than zero",
type: "number",
});
});
test("can't convert to JSON Schema by default", () => {
expect(() => {
t.toJSONSchema(">0", t.num.validate("greater than zero", (num) => num > 0))
}).toThrow();
});
test("passes when the fn returns true", () => {
const check = t.num.validate("between five and ten", (num) => {
return num > 5 && num < 10;
});
check.assert(6);
});
test("fails when the fn returns false", () => {
const check= t.num.validate("non-zero", num => num !== 0);
expect(() => {
check.assert(0);
}).toThrow();
});
test("fails when the fn throws", () => {
const check = t.num.validate("never", (_) => {
throw new Error();
});
expect(check.check(0)).toBeInstanceOf(t.Err);
});