feat(generics): Improve assertion and matcher type definitions by adding execution mode generics for better async/sync support
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { Assertion, AnyMatcher, AnythingMatcher } from './smartexpect.classes.assertion.js';
|
||||
import type { TExecutionType } from './types.js';
|
||||
// import type { TMatcher } from './smartexpect.classes.assertion.js'; // unused
|
||||
|
||||
/**
|
||||
@ -12,12 +13,12 @@ import { Assertion, AnyMatcher, AnythingMatcher } from './smartexpect.classes.as
|
||||
* 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> {
|
||||
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>(value, mode);
|
||||
return new Assertion<T, TExecutionType>(value, mode);
|
||||
}
|
||||
/**
|
||||
* Register custom matchers.
|
||||
|
Reference in New Issue
Block a user