BREAKING CHANGE(docs): Update documentation and examples to unify async and sync assertions, add custom matcher guides, and update package configuration

This commit is contained in:
2025-04-28 19:10:27 +00:00
parent 6f1e37cf56
commit 47458118a6
19 changed files with 606 additions and 663 deletions

44
ts/namespaces/array.ts Normal file
View File

@ -0,0 +1,44 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for array-specific matchers
*/
export class ArrayMatchers<T> {
constructor(private assertion: Assertion<T[]>) {}
toBeArray() {
return this.assertion.toBeArray();
}
toHaveLength(length: number) {
return this.assertion.toHaveLength(length);
}
toContain(item: T) {
return this.assertion.toContain(item);
}
toContainEqual(item: T) {
return this.assertion.toContainEqual(item);
}
toContainAll(items: T[]) {
return this.assertion.toContainAll(items);
}
toExclude(item: T) {
return this.assertion.toExclude(item);
}
toBeEmptyArray() {
return this.assertion.toBeEmptyArray();
}
toHaveLengthGreaterThan(length: number) {
return this.assertion.toHaveLengthGreaterThan(length);
}
toHaveLengthLessThan(length: number) {
return this.assertion.toHaveLengthLessThan(length);
}
}

24
ts/namespaces/boolean.ts Normal file
View File

@ -0,0 +1,24 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for boolean-specific matchers
*/
export class BooleanMatchers {
constructor(private assertion: Assertion<boolean>) {}
toBeTrue() {
return this.assertion.toBeTrue();
}
toBeFalse() {
return this.assertion.toBeFalse();
}
toBeTruthy() {
return this.assertion.toBeTruthy();
}
toBeFalsy() {
return this.assertion.toBeFalsy();
}
}

20
ts/namespaces/date.ts Normal file
View File

@ -0,0 +1,20 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for date-specific matchers
*/
export class DateMatchers {
constructor(private assertion: Assertion<Date>) {}
toBeDate() {
return this.assertion.toBeDate();
}
toBeBeforeDate(date: Date) {
return this.assertion.toBeBeforeDate(date);
}
toBeAfterDate(date: Date) {
return this.assertion.toBeAfterDate(date);
}
}

12
ts/namespaces/function.ts Normal file
View File

@ -0,0 +1,12 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for function-specific matchers
*/
export class FunctionMatchers {
constructor(private assertion: Assertion<Function>) {}
toThrow(expectedError?: any) {
return this.assertion.toThrow(expectedError);
}
}

8
ts/namespaces/index.ts Normal file
View File

@ -0,0 +1,8 @@
export { StringMatchers } from './string.js';
export { ArrayMatchers } from './array.js';
export { NumberMatchers } from './number.js';
export { BooleanMatchers } from './boolean.js';
export { ObjectMatchers } from './object.js';
export { FunctionMatchers } from './function.js';
export { DateMatchers } from './date.js';
export { TypeMatchers } from './type.js';

32
ts/namespaces/number.ts Normal file
View File

@ -0,0 +1,32 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for number-specific matchers
*/
export class NumberMatchers {
constructor(private assertion: Assertion<number>) {}
toBeGreaterThan(value: number) {
return this.assertion.toBeGreaterThan(value);
}
toBeLessThan(value: number) {
return this.assertion.toBeLessThan(value);
}
toBeGreaterThanOrEqual(value: number) {
return this.assertion.toBeGreaterThanOrEqual(value);
}
toBeLessThanOrEqual(value: number) {
return this.assertion.toBeLessThanOrEqual(value);
}
toBeCloseTo(value: number, precision?: number) {
return this.assertion.toBeCloseTo(value, precision);
}
/** Equality check for numbers */
toEqual(value: number) {
return this.assertion.toEqual(value);
}
}

39
ts/namespaces/object.ts Normal file
View File

@ -0,0 +1,39 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for object-specific matchers
*/
export class ObjectMatchers<T extends object> {
constructor(private assertion: Assertion<T>) {}
toEqual(expected: any) {
return this.assertion.toEqual(expected);
}
toMatchObject(expected: object) {
return this.assertion.toMatchObject(expected);
}
toBeInstanceOf(constructor: any) {
return this.assertion.toBeInstanceOf(constructor);
}
toHaveProperty(property: string, value?: any) {
return this.assertion.toHaveProperty(property, value);
}
toHaveDeepProperty(path: string[]) {
return this.assertion.toHaveDeepProperty(path);
}
toBeNull() {
return this.assertion.toBeNull();
}
toBeUndefined() {
return this.assertion.toBeUndefined();
}
toBeNullOrUndefined() {
return this.assertion.toBeNullOrUndefined();
}
}

32
ts/namespaces/string.ts Normal file
View File

@ -0,0 +1,32 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for string-specific matchers
*/
export class StringMatchers {
constructor(private assertion: Assertion<string>) {}
toStartWith(prefix: string) {
return this.assertion.toStartWith(prefix);
}
toEndWith(suffix: string) {
return this.assertion.toEndWith(suffix);
}
toInclude(substring: string) {
return this.assertion.toInclude(substring);
}
toMatch(regex: RegExp) {
return this.assertion.toMatch(regex);
}
toBeOneOf(values: string[]) {
return this.assertion.toBeOneOf(values);
}
/** Length check for strings */
toHaveLength(length: number) {
return this.assertion.toHaveLength(length);
}
}

28
ts/namespaces/type.ts Normal file
View File

@ -0,0 +1,28 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
/**
* Namespace for type-based matchers
*/
export class TypeMatchers {
constructor(private assertion: Assertion<any>) {}
toBeTypeofString() {
return this.assertion.toBeTypeofString();
}
toBeTypeofNumber() {
return this.assertion.toBeTypeofNumber();
}
toBeTypeofBoolean() {
return this.assertion.toBeTypeofBoolean();
}
toBeTypeOf(typeName: string) {
return this.assertion.toBeTypeOf(typeName);
}
toBeDefined() {
return this.assertion.toBeDefined();
}
}