feat(Assertion): Add missing alias methods for length and emptiness checks and update documentation

This commit is contained in:
2025-05-23 18:30:47 +00:00
parent dbec1d3e4a
commit 0054271de6
4 changed files with 112 additions and 2 deletions

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartexpect',
version: '2.4.2',
version: '2.5.0',
description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.'
}

View File

@@ -378,6 +378,54 @@ export class Assertion<T = unknown, M extends TExecutionType = 'sync'> {
public toBeTypeOf(typeName: string) { return this.type.toBeTypeOf(typeName); }
public toBeDefined() { return this.type.toBeDefined(); }
// Additional missing direct aliases for completeness
// String/Array namespace - intelligently delegate based on value type
public toHaveLength(length: number) {
// Determine if value is string or array and delegate accordingly
const value = this.getObjectToTestReference();
if (typeof value === 'string') {
return this.string.toHaveLength(length);
} else if (Array.isArray(value)) {
return this.array.toHaveLength(length);
} else {
return this.customAssertion(
() => false,
'Expected value to be string or array to check length'
);
}
}
public toBeEmpty() {
// Determine if value is string or array and delegate accordingly
const value = this.getObjectToTestReference();
if (typeof value === 'string') {
return this.string.toBeEmpty();
} else if (Array.isArray(value)) {
return this.array.toBeEmpty();
} else {
return this.customAssertion(
() => false,
'Expected value to be string or array to check if empty'
);
}
}
// Number namespace
public toBeNaN() { return this.number.toBeNaN(); }
public toBeFinite() { return this.number.toBeFinite(); }
public toBeWithinRange(min: number, max: number) { return this.number.toBeWithinRange(min, max); }
// Array namespace length comparisons
public toHaveLengthGreaterThan(length: number) { return this.array.toHaveLengthGreaterThan(length); }
public toHaveLengthLessThan(length: number) { return this.array.toHaveLengthLessThan(length); }
// Object namespace
public toHaveKeys(keys: string[]) { return this.object.toHaveKeys(keys); }
public toHaveOwnKeys(keys: string[]) { return this.object.toHaveOwnKeys(keys); }
// Function namespace
public toThrowErrorMatching(regex: RegExp) { return this.function.toThrowErrorMatching(regex); }
public toThrowErrorWithMessage(message: string) { return this.function.toThrowErrorWithMessage(message); }
// Namespaced matcher accessors
/** String-specific matchers */
public get string(): StringMatchers<M> {