UNPKG

@push.rocks/smartexpect

Version:

A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.

159 lines (158 loc) 9.1 kB
import { StringMatchers, ArrayMatchers, NumberMatchers, BooleanMatchers, ObjectMatchers, FunctionMatchers, DateMatchers, TypeMatchers } from './namespaces/index.js'; /** * Definition of a custom matcher function. * Should return an object with `pass` and optional `message`. */ import type { TMatcher, TExecutionType } from './types.js'; /** * Core assertion class. Generic over the current value type T. */ /** * Internal matcher classes for expect.any and expect.anything */ export declare class AnyMatcher { expectedConstructor: any; constructor(expectedConstructor: any); } export declare class AnythingMatcher { } export declare class Assertion<T = unknown, M extends TExecutionType = 'sync'> { executionMode: M; baseReference: any; propertyDrillDown: Array<string | number>; private notSetting; private timeoutSetting; /** Registry of user-defined custom matchers */ private static customMatchers; /** Flag for Promise rejection assertions */ private isRejects; /** Flag for Promise resolution assertions (default for async) */ private isResolves; private failMessage; private successMessage; /** Computed negation failure message for the current assertion */ private negativeMessage; constructor(baseReferenceArg: any, executionModeArg: M); /** * Register custom matchers to be available on all assertions. * @param matchers An object whose keys are matcher names and values are matcher functions. */ static extend(matchers: Record<string, TMatcher>): void; private getObjectToTestReference; private formatDrillDown; private formatValue; private createErrorMessage; /** * Compute a negated failure message by inserting 'not' into the positive message. */ private computeNegationMessage; get not(): this; /** * Assert that a Promise resolves. */ /** * Switch to async (resolve) mode. Subsequent matchers return Promises. */ get resolves(): Assertion<T, 'async'>; /** * Assert that a Promise rejects. */ /** * Switch to async (reject) mode. Subsequent matchers return Promises. */ get rejects(): Assertion<T, 'async'>; /** * @deprecated use `.withTimeout(ms)` instead for clarity * Set a timeout (in ms) for async assertions (Promise must settle before timeout). */ timeout(millisArg: number): this; /** * Set a timeout (in ms) for async assertions (Promise must settle before timeout). */ withTimeout(millisArg: number): this; setFailMessage(failMessageArg: string): this; setSuccessMessage(successMessageArg: string): this; private runCheck; /** * Execute a custom assertion. Returns a Promise in async mode, else returns this. */ customAssertion(assertionFunction: (value: any) => boolean, errorMessage: string | ((value: any) => string)): M extends 'async' ? Promise<Assertion<T, M>> : Assertion<T, M>; /** * Drill into a property of an object. * @param propertyName Name of the property to navigate into. * @returns Assertion of the property type. */ property<K extends keyof NonNullable<T>>(propertyName: K): Assertion<NonNullable<T>[K], M>; /** * Drill into an array element by index. * @param index Index of the array item. * @returns Assertion of the element type. */ arrayItem(index: number): Assertion<T extends Array<infer U> ? U : unknown, M>; log(): this; toEqual(expected: any): M extends "async" ? Promise<Assertion<T, M>> : Assertion<T, M>; toBeTrue(): M extends "async" ? Promise<Assertion<boolean, M>> : Assertion<boolean, M>; toBeFalse(): M extends "async" ? Promise<Assertion<boolean, M>> : Assertion<boolean, M>; toBeTruthy(): M extends "async" ? Promise<Assertion<boolean, M>> : Assertion<boolean, M>; toBeFalsy(): M extends "async" ? Promise<Assertion<boolean, M>> : Assertion<boolean, M>; toThrow(expectedError?: any): M extends "async" ? Promise<Assertion<Function, M>> : Assertion<Function, M>; toBeGreaterThan(value: number): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeLessThan(value: number): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeGreaterThanOrEqual(value: number): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeLessThanOrEqual(value: number): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeCloseTo(value: number, precision?: number): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeArray(): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toContain(item: any): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toContainEqual(item: any): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toContainAll(items: any[]): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toExclude(item: any): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toBeEmptyArray(): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toStartWith(prefix: string): M extends "async" ? Promise<Assertion<string, M>> : Assertion<string, M>; toEndWith(suffix: string): M extends "async" ? Promise<Assertion<string, M>> : Assertion<string, M>; toInclude(substring: string): M extends "async" ? Promise<Assertion<string, M>> : Assertion<string, M>; toMatch(regex: RegExp): M extends "async" ? Promise<Assertion<string, M>> : Assertion<string, M>; toBeOneOf(values: any[]): M extends "async" ? Promise<Assertion<string, M>> : Assertion<string, M>; toHaveProperty(property: string, value?: any): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toHaveOwnProperty(property: string, value?: any): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toMatchObject(expected: object): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeInstanceOf(constructor: any): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toHaveDeepProperty(path: string[]): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeNull(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeUndefined(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeNullOrUndefined(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeDate(): M extends "async" ? Promise<Assertion<Date, M>> : Assertion<Date, M>; toBeBeforeDate(date: Date): M extends "async" ? Promise<Assertion<Date, M>> : Assertion<Date, M>; toBeAfterDate(date: Date): M extends "async" ? Promise<Assertion<Date, M>> : Assertion<Date, M>; toBeTypeofString(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeTypeofNumber(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeTypeofBoolean(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeTypeOf(typeName: string): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toBeDefined(): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toHaveLength(length: number): M extends "async" ? Promise<Assertion<T, M>> : Assertion<T, M>; toBeEmpty(): M extends "async" ? Promise<Assertion<T, M>> : Assertion<T, M>; toBeNaN(): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeFinite(): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toBeWithinRange(min: number, max: number): M extends "async" ? Promise<Assertion<number, M>> : Assertion<number, M>; toHaveLengthGreaterThan(length: number): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toHaveLengthLessThan(length: number): M extends "async" ? Promise<Assertion<any[], M>> : Assertion<any[], M>; toHaveKeys(keys: string[]): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toHaveOwnKeys(keys: string[]): M extends "async" ? Promise<Assertion<any, M>> : Assertion<any, M>; toThrowErrorMatching(regex: RegExp): M extends "async" ? Promise<Assertion<Function, M>> : Assertion<Function, M>; toThrowErrorWithMessage(message: string): M extends "async" ? Promise<Assertion<Function, M>> : Assertion<Function, M>; /** String-specific matchers */ get string(): StringMatchers<M>; /** Array-specific matchers */ get array(): ArrayMatchers<any, M>; /** Number-specific matchers */ get number(): NumberMatchers<M>; /** Boolean-specific matchers */ get boolean(): BooleanMatchers<M>; /** Object-specific matchers */ get object(): ObjectMatchers<any, M>; /** Function-specific matchers */ get function(): FunctionMatchers<M>; /** Date-specific matchers */ get date(): DateMatchers<M>; /** Type-based matchers */ get type(): TypeMatchers<M>; }