2025-04-28 20:42:58 +00:00
|
|
|
import { Assertion, AnyMatcher, AnythingMatcher } from './smartexpect.classes.assertion.js';
|
2025-04-28 19:10:27 +00:00
|
|
|
// import type { TMatcher } from './smartexpect.classes.assertion.js'; // unused
|
2022-01-21 03:33:24 +01:00
|
|
|
|
2025-04-28 19:10:27 +00:00
|
|
|
/**
|
|
|
|
* 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>;
|
|
|
|
export function expect<T>(value: T): Assertion<T>;
|
|
|
|
export function expect<T>(value: any): Assertion<T> {
|
|
|
|
const isThenable = value != null && typeof (value as any).then === 'function';
|
|
|
|
const mode: 'sync' | 'async' = isThenable ? 'async' : 'sync';
|
|
|
|
return new Assertion<T>(value, mode);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Register custom matchers.
|
|
|
|
*/
|
|
|
|
export namespace expect {
|
|
|
|
export const extend = Assertion.extend;
|
2025-04-28 20:42:58 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2025-04-28 19:10:27 +00:00
|
|
|
}
|
2022-01-21 03:33:24 +01:00
|
|
|
|
2025-04-28 19:10:27 +00:00
|
|
|
/**
|
|
|
|
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
|
|
|
|
*/
|
2022-01-21 03:33:24 +01:00
|
|
|
export const expectAsync = (baseArg: any) => {
|
2025-04-28 19:10:27 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn('[DEPRECATED] expectAsync() is deprecated. Use expect(...).resolves / .rejects');
|
|
|
|
return new Assertion<any>(baseArg, 'async');
|
2022-07-24 12:45:29 +02:00
|
|
|
};
|
2024-08-24 01:36:23 +02:00
|
|
|
|