smartexpect/ts/index.ts

42 lines
1.3 KiB
TypeScript

import { Assertion, AnyMatcher, AnythingMatcher } from './smartexpect.classes.assertion.js';
import type { TExecutionType } from './types.js';
// import type { TMatcher } from './smartexpect.classes.assertion.js'; // unused
/**
* Primary entry point for assertions.
* Automatically detects Promises to support async assertions.
*/
/**
* The `expect` function interface. Supports custom matchers via .extend.
*/
/**
* Entry point for assertions.
* Automatically detects Promises to support async assertions.
*/
export function expect<T>(value: Promise<T>): Assertion<T, 'async'>;
export function expect<T>(value: T): Assertion<T, 'sync'>;
export function expect<T>(value: any): Assertion<T, TExecutionType> {
const isThenable = value != null && typeof (value as any).then === 'function';
const mode: 'sync' | 'async' = isThenable ? 'async' : 'sync';
return new Assertion<T, TExecutionType>(value, mode);
}
/**
* Register custom matchers.
*/
export namespace expect {
export const extend = Assertion.extend;
/**
* Matcher for a specific constructor. Passes if value is instance of given constructor.
*/
export function any(constructor: any) {
return new AnyMatcher(constructor);
}
/**
* Matcher for any defined value (not null or undefined).
*/
export function anything() {
return new AnythingMatcher();
}
}