UNPKG

types-testing

Version:

Test TypeScript types at test runner runtime - Works seamlessly with Jest, Vitest, and Bun.

144 lines (143 loc) 4.19 kB
import { Factory } from "../index.js"; import ts from "typescript"; const toBeAny = Factory.assertionWithoutTypeArgument((_, received) => { return (received.flags & ts.TypeFlags.Any) !== 0; }); const toBeUnknown = Factory.assertionWithoutTypeArgument( (_, received) => { return (received.flags & ts.TypeFlags.Unknown) !== 0; } ); const toBeNever = Factory.assertionWithoutTypeArgument((_, received) => { return (received.flags & ts.TypeFlags.Never) !== 0; }); const toBeVoid = Factory.assertionWithoutTypeArgument((_, received) => { return (received.flags & ts.TypeFlags.Void) !== 0; }); const toBeUndefined = Factory.assertionWithoutTypeArgument( (_, received) => { return (received.flags & ts.TypeFlags.Undefined) !== 0; } ); const toBeNull = Factory.assertionWithoutTypeArgument((_, received) => { return (received.flags & ts.TypeFlags.Null) !== 0; }); const toBeString = Factory.assertionWithoutTypeArgument( (_, received) => { return (received.flags & ts.TypeFlags.String) !== 0 || received.isStringLiteral(); } ); const toBeStringLiteral = Factory.assertionWithoutTypeArgument( (_, received) => { return received.isStringLiteral(); } ); const toBeNumber = Factory.assertionWithoutTypeArgument( (_, received) => { return (received.flags & ts.TypeFlags.Number) !== 0 || received.isNumberLiteral(); } ); const toBeNumberLiteral = Factory.assertionWithoutTypeArgument( (_, received) => { return received.isNumberLiteral(); } ); const toBeBoolean = Factory.assertionWithoutTypeArgument( (_, received) => { const flags = ts.TypeFlags.Boolean | ts.TypeFlags.BooleanLiteral; return (received.flags & flags) !== 0; } ); const toBeBooleanLiteral = Factory.assertionWithoutTypeArgument( (_, received) => { return (received.flags & ts.TypeFlags.BooleanLiteral) !== 0; } ); const toBeTrue = Factory.assertionWithoutTypeArgument( (checker, received) => { return received === checker.getTrueType() || (received.flags & ts.TypeFlags.BooleanLiteral) !== 0 && received.intrinsicName === "true"; } ); const toBeFalse = Factory.assertionWithoutTypeArgument( (checker, received) => { return received === checker.getFalseType() || (received.flags & ts.TypeFlags.BooleanLiteral) !== 0 && received.intrinsicName === "false"; } ); const toBeObject = Factory.assertionWithoutTypeArgument( (_, received) => { return (received.flags & ts.TypeFlags.Object) !== 0; } ); const toBeArray = Factory.assertionWithoutTypeArgument( (checker, received) => { return checker.isArrayType(received) || checker.isTupleType(received); } ); const toBeTuple = Factory.assertionWithoutTypeArgument( (checker, received) => { return checker.isTupleType(received); } ); const toBeFunction = Factory.assertionWithoutTypeArgument( (checker, received) => { const callSignatures = checker.getSignaturesOfType( received, ts.SignatureKind.Call ); return callSignatures.length > 0; } ); const toBeClass = Factory.assertionWithoutTypeArgument((_, received) => { return received.isClass(); }); const toBeUnion = Factory.assertionWithoutTypeArgument((_, received) => { return received.isUnion(); }); const toBeIntersection = Factory.assertionWithoutTypeArgument( (_, received) => { return received.isIntersection(); } ); const toBe = Factory.assertionWithTypeArgument( (checker, received, expected) => { const isAny = { received: toBeAny(checker, received), expected: toBeAny(checker, expected) }; if (isAny.received && isAny.expected) { return true; } if (isAny.received || isAny.expected) { return false; } return checker.isTypeAssignableTo(received, expected) && checker.isTypeAssignableTo(expected, received); } ); const toEqual = toBe; const toStrictEqual = toBe; export { toBe, toBeAny, toBeArray, toBeBoolean, toBeBooleanLiteral, toBeClass, toBeFalse, toBeFunction, toBeIntersection, toBeNever, toBeNull, toBeNumber, toBeNumberLiteral, toBeObject, toBeString, toBeStringLiteral, toBeTrue, toBeTuple, toBeUndefined, toBeUnion, toBeUnknown, toBeVoid, toEqual, toStrictEqual };