///
import * as Hoek from '@hapi/hoek';
// Internal helpers
type Class = new (...args: any[]) => T;
type UnpackArray = T extends (infer U)[] ? U : T;
type RecursivePartial = T extends object ? {
[P in keyof T]?:
T[P] extends Array ? Array> :
RecursivePartial;
} : T;
type Loosely = T extends object ? RecursivePartial & { [key: string]: any } : T;
/**
* Configure code behavior
*/
export const settings: Settings;
export interface Settings {
/**
* Truncate long assertion error messages for readability.
*
* @default false
*/
truncateMessages?: boolean;
/**
* Ignore object prototypes when doing a deep comparison.
*
* @defaults false
*/
comparePrototypes?: boolean;
}
/**
* Makes the test fail.
*
* @param message - the error message generated.
*/
export function fail(message?: string): void;
/**
* Returns the total number of assertions created using the `expect()` method.
*
* @returns total number of assertions.
*/
export function count(): number;
/**
* Returns an array of the locations where incomplete assertions were declared or `null` if no incomplete assertions found.
*
* @returns array of incomplete assertion locations.
*/
export function incomplete(): string[] | null;
/**
* Returns the filename, line number, and column number of where the `error` was created. If no error is provided, the current location returned.
*
* @param error - an error object.
*
* @returns the location where the error was thrown.
*/
export function thrownAt(error?: Error): thrownAt.Location;
export namespace thrownAt {
interface Location {
filename: string;
line: string;
column: string;
}
}
/**
* Declares an assertion chain.
*
* @param value - the value being asserted.
* @param prefix - a string prefix added to error messages.
*
* @returns Assertion object.
*/
export function expect(value: T, prefix?: string): expect.Assertion;
declare namespace expect {
type Assertion =
TTest extends Function ? expect.FunctionAssertion :
TTest extends string ? expect.StringAssertion :
TTest extends number | bigint ? expect.NumberAssertion :
TTest extends Promise ? expect.PromiseAssertion :
expect.BaseAssertion;
interface BaseAssertion {
// Grammar
a: this;
an: this;
and: this;
at: this;
be: this;
have: this;
in: this;
to: this;
// Flags
/**
* Inverses the expected result of the assertion chain.
*/
not: TTest extends Function ? expect.Not_FunctionAssertion :
TTest extends Promise ? expect.Not_PromiseAssertion :
this;
/**
* Requires that inclusion matches appear only once in the provided value.
*/
once: this;
/**
* Requires that only the provided elements appear in the provided value.
*/
only: this;
/**
* Allows a partial match when asserting inclusion instead of a full comparison.
*/
part: this;
/**
* Performs a comparison using strict equality (===) instead of a deep comparison.
*/
shallow: this;
// Types
/**
* Asserts that the reference value is an arguments object.
*
* @returns assertion chain object.
*/
arguments(): Assertion;
/**
* Asserts that the reference value is an Array.
*
* @returns assertion chain object.
*/
array(): Assertion;
/**
* Asserts that the reference value is a boolean.
*
* @returns assertion chain object.
*/
boolean(): Assertion;
/**
* Asserts that the reference value is a Buffer.
*
* @returns assertion chain object.
*/
buffer(): Assertion;
/**
* Asserts that the reference value is a Date
*
* @returns assertion chain object.
*/
date(): Assertion;
/**
* Asserts that the reference value is an error.
*
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns assertion chain object.
*/
error(type: Class, message?: string | RegExp): Assertion;
error(message?: string | RegExp): Assertion;
/**
* Asserts that the reference value is a function.
*
* @returns assertion chain object.
*/
function(): Assertion;
/**
* Asserts that the reference value is a number.
*
* @returns assertion chain object.
*/
number(): Assertion;
/**
* Asserts that the reference value is a RegExp.
*
* @returns assertion chain object.
*/
regexp(): Assertion;
/**
* Asserts that the reference value is a string.
*
* @returns assertion chain object.
*/
string(): Assertion;
/**
* Asserts that the reference value is an object (excluding array, buffer, or other native objects).
*
* @returns assertion chain object.
*/
object(): Assertion;
// Values
/**
* Asserts that the reference value is true.
*
* @returns assertion chain object.
*/
true(): Assertion;
/**
* Asserts that the reference value is false.
*
* @returns assertion chain object.
*/
false(): Assertion;
/**
* Asserts that the reference value is null.
*
* @returns assertion chain object.
*/
null(): Assertion;
/**
* Asserts that the reference value is undefined.
*
* @returns assertion chain object.
*/
undefined(): Assertion;
/**
* Asserts that the reference value is `NaN`.
*
* @returns assertion chain object.
*/
NaN(): Assertion;
// Tests
/**
* Asserts that the reference value (a string, array, or object) includes the provided values.
*
* @param values - the values to include.
*
* @returns assertion chain object.
*/
include(values: UnpackArray | Loosely[]>): Assertion;
include(values: string | string[]): Assertion;
/**
* Asserts that the reference value (a string, array, or object) includes the provided values.
*
* @param values - the values to include.
*
* @returns assertion chain object.
*/
includes(values: UnpackArray | Loosely[]>): Assertion;
includes(values: string | string[]): Assertion;
/**
* Asserts that the reference value (a string, array, or object) includes the provided values.
*
* @param values - the values to include.
*
* @returns assertion chain object.
*/
contain(values: UnpackArray | Loosely[]>): Assertion;
contain(values: string | string[]): Assertion;
/**
* Asserts that the reference value (a string, array, or object) includes the provided values.
*
* @param values - the values to include.
*
* @returns assertion chain object.
*/
contains(values: UnpackArray | Loosely[]>): Assertion;
contains(values: string | string[]): Assertion;
/**
* Asserts that the reference value exists (not null or undefined).
*
* @returns assertion chain object.
*/
exist(): Assertion;
/**
* Asserts that the reference value exists (not null or undefined).
*
* @returns assertion chain object.
*/
exists(): Assertion;
/**
* Asserts that the reference value has a length property equal to zero or is an object with no keys.
*
* @returns assertion chain object.
*/
empty(): Assertion;
/**
* Asserts that the reference value has a length property matching the provided size or an object with the specified number of keys.
*
* @param size - the required length.
*
* @returns assertion chain object.
*/
length(size: T extends string | Buffer | object | any[] ? number : never): Assertion;
/**
* Asserts that the reference value equals the provided value.
*
* @param value - the value to match.
* @param options - comparison options.
*
* @returns assertion chain object.
*/
equal(value: RecursivePartial, options?: Hoek.deepEqual.Options): Assertion;
/**
* Asserts that the reference value equals the provided value.
*
* @param value - the value to match.
* @param options - comparison options.
*
* @returns assertion chain object.
*/
equals(value: RecursivePartial, options?: Hoek.deepEqual.Options): Assertion;
/**
* Asserts that the reference value has the provided instanceof value.
*
* @param type - the constructor function to be an instance of.
*/
instanceof(type: Class): Assertion;
/**
* Asserts that the reference value has the provided instanceof value.
*
* @param type - the constructor function to be an instance of.
*/
instanceOf(type: Class): Assertion;
/**
* Asserts that the reference value's toString() representation matches the provided regular expression.
*
* @param regex - the pattern to match.
*
* @returns assertion chain object.
*/
match(regex: RegExp): Assertion;
/**
* Asserts that the reference value's toString() representation matches the provided regular expression.
*
* @param regex - the pattern to match.
*
* @returns assertion chain object.
*/
matches(regex: RegExp): Assertion;
/**
* Asserts that the reference value satisfies the provided validator function.
*
* @param validator
*
* @returns assertion chain object.
*/
satisfy(validator: (value: T) => boolean): Assertion;
/**
* Asserts that the reference value satisfies the provided validator function.
*
* @param validator
*
* @returns assertion chain object.
*/
satisfies(validator: (value: T) => boolean): Assertion;
}
interface FunctionAssertion extends BaseAssertion {
/**
* Asserts that the function reference value throws an exception when called.
*
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns thrown value.
*/
throw(type: Class, message?: string | RegExp): E;
throw(message?: string | RegExp): E;
/**
* Asserts that the function reference value throws an exception when called.
*
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns thrown value.
*/
throws(type: Class, message?: string | RegExp): E;
throws(message?: string | RegExp): E;
}
interface Not_FunctionAssertion extends BaseAssertion {
/**
* Asserts that the function reference value throws an exception when called.
*
* @returns assertion chain object.
*/
throw(): Assertion;
throw(): Assertion;
/**
* Asserts that the function reference value throws an exception when called.
*
* @returns assertion chain object.
*/
throws(): Assertion;
throws(): Assertion;
}
interface StringAssertion extends BaseAssertion {
/**
* Asserts that the reference value (a string) starts with the provided value.
*
* @param value - the value to start with.
*
* @returns assertion chain object.
*/
startWith(value: string): Assertion;
/**
* Asserts that the reference value (a string) starts with the provided value.
*
* @param value - the value to start with.
*
* @returns assertion chain object.
*/
startsWith(value: string): Assertion;
/**
* Asserts that the reference value (a string) ends with the provided value.
*
* @param value - the value to end with.
*
* @returns assertion chain object.
*/
endWith(value: string): Assertion;
/**
* Asserts that the reference value (a string) ends with the provided value.
*
* @param value - the value to end with.
*
* @returns assertion chain object.
*/
endsWith(value: string): Assertion;
}
interface NumberAssertion extends BaseAssertion {
/**
* Asserts that the reference value is greater than (>) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
above(value: T): Assertion;
/**
* Asserts that the reference value is within a delta difference from the provided value.
*
* @param value - the value to compare to.
* @param delta - the delta +/- range value.
*
* @returns assertion chain object.
*/
about(value: T, delta: number): Assertion;
/**
* Asserts that the reference value is greater than (>) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
greaterThan(value: T): Assertion;
/**
* Asserts that the reference value is at least (>=) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
least(value: T): Assertion;
/**
* Asserts that the reference value is at least (>=) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
min(value: T): Assertion;
/**
* Asserts that the reference value is less than (<) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
below(value: T): Assertion;
/**
* Asserts that the reference value is less than (<) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
lessThan(value: T): Assertion;
/**
* Asserts that the reference value is at most (<=) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
most(value: T): Assertion;
/**
* Asserts that the reference value is at most (<=) the provided value.
*
* @param value - the value to compare to.
*
* @returns assertion chain object.
*/
max(value: T): Assertion;
/**
* Asserts that the reference value is within (from <= value <= to) the provided values.
*
* @param from - the value to be equal to or above.
* @param to - the value to be equal to or below.
*
* @returns assertion chain object.
*/
within(from: T, to: T): Assertion;
/**
* Asserts that the reference value is within (from <= value <= to) the provided values.
*
* @param from - the value to be equal to or above.
* @param to - the value to be equal to or below.
*
* @returns assertion chain object.
*/
range(from: T, to: T): Assertion;
/**
* Asserts that the reference value is between but not equal (from < value < to) the provided values.
*
* @param from - the value to be above.
* @param to - the value to be below.
*
* @returns assertion chain object.
*/
between(from: T, to: T): Assertion;
/**
* Asserts that the reference value is about the provided value within a delta margin of difference.
*
* @param value - the value to be near.
* @param delta - the max distance to be from the value.
*
* @returns assertion chain object.
*/
about(value: T extends number ? T : never, delta: T extends number ? T : never): Assertion;
}
interface PromiseAssertion extends BaseAssertion {
/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns a promise resolving to the rejected value.
*/
reject(type: Class, message?: string | RegExp): Promise;
reject(message?: string | RegExp): Promise;
/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns a promise resolving to the rejected value.
*/
rejects(type: Class, message?: string | RegExp): Promise;
rejects(message?: string | RegExp): Promise;
}
interface Not_PromiseAssertion extends BaseAssertion {
/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @returns a promise resolving to null.
*/
reject(): Promise;
/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @returns a promise resolving to null.
*/
rejects(): Promise;
}
}