Compare commits

...

4 Commits

5 changed files with 41 additions and 39 deletions

View File

@ -1,5 +1,18 @@
# Changelog # Changelog
## 2025-04-29 - 2.1.2 - fix(ts/index.ts)
Remove deprecated expectAsync function and advise using .resolves/.rejects on expect for async assertions
- Deleted the redundant expectAsync export in ts/index.ts
- Users should now call expect(...).resolves or expect(...).rejects for asynchronous assertions
## 2025-04-29 - 2.1.1 - fix(Assertion)
Improve chainability by fixing return types in assertion methods
- Update runCheck method to explicitly return the correct chainable type for both async and sync assertions
- Ensure customAssertion propagates the chainable Assertion instance
- Refactor internal promise handling for clarity and consistency
## 2025-04-28 - 2.1.0 - feat(core) ## 2025-04-28 - 2.1.0 - feat(core)
Add new matchers and improve negation messaging Add new matchers and improve negation messaging

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartexpect", "name": "@push.rocks/smartexpect",
"version": "2.1.0", "version": "2.1.2",
"private": false, "private": false,
"description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.", "description": "A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

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

View File

@ -38,18 +38,3 @@ export namespace expect {
} }
} }
/**
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
*/
/**
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
*/
/**
* @deprecated Use `expect(...)` with `.resolves` or `.rejects` instead.
*/
export const expectAsync = (baseArg: any) => {
// eslint-disable-next-line no-console
console.warn('[DEPRECATED] expectAsync() is deprecated. Use expect(...).resolves / .rejects');
return new Assertion<any>(baseArg, 'async');
};

View File

@ -203,7 +203,7 @@ export class Assertion<T = unknown> {
return this; return this;
} }
private runCheck(checkFunction: () => any) { private runCheck(checkFunction: () => any): Assertion<T> | Promise<Assertion<T>> {
const runDirectOrNegated = (checkFunction: () => any) => { const runDirectOrNegated = (checkFunction: () => any) => {
if (!this.notSetting) { if (!this.notSetting) {
return checkFunction(); return checkFunction();
@ -223,7 +223,7 @@ export class Assertion<T = unknown> {
}; };
if (this.executionMode === 'async') { if (this.executionMode === 'async') {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer<Assertion<T>>();
const isThenable = this.baseReference && typeof (this.baseReference as any).then === 'function'; const isThenable = this.baseReference && typeof (this.baseReference as any).then === 'function';
if (!isThenable) { if (!isThenable) {
done.reject(new Error(`Expected a Promise but received: ${this.formatValue(this.baseReference)}`)); done.reject(new Error(`Expected a Promise but received: ${this.formatValue(this.baseReference)}`));
@ -244,8 +244,8 @@ export class Assertion<T = unknown> {
(err: any) => { (err: any) => {
this.baseReference = err; this.baseReference = err;
try { try {
const ret = runDirectOrNegated(checkFunction); runDirectOrNegated(checkFunction);
done.resolve(ret); done.resolve(this);
} catch (e: any) { } catch (e: any) {
done.reject(e); done.reject(e);
} }
@ -256,8 +256,8 @@ export class Assertion<T = unknown> {
(res: any) => { (res: any) => {
this.baseReference = res; this.baseReference = res;
try { try {
const ret = runDirectOrNegated(checkFunction); runDirectOrNegated(checkFunction);
done.resolve(ret); done.resolve(this);
} catch (e: any) { } catch (e: any) {
done.reject(e); done.reject(e);
} }
@ -267,15 +267,18 @@ export class Assertion<T = unknown> {
} }
); );
} }
return done.promise; // return a promise resolving to this for chaining
return done.promise.then(() => this);
} }
return runDirectOrNegated(checkFunction); // sync: run and return this for chaining
runDirectOrNegated(checkFunction);
return this;
} }
public customAssertion( public customAssertion(
assertionFunction: (value: any) => boolean, assertionFunction: (value: any) => boolean,
errorMessage: string | ((value: any) => string) errorMessage: string | ((value: any) => string)
) { ): Assertion<T> | Promise<Assertion<T>> {
// Prepare negation message based on the positive error template, if static // Prepare negation message based on the positive error template, if static
if (typeof errorMessage === 'string') { if (typeof errorMessage === 'string') {
this.negativeMessage = this.computeNegationMessage(errorMessage); this.negativeMessage = this.computeNegationMessage(errorMessage);
@ -359,6 +362,7 @@ export class Assertion<T = unknown> {
public toBeTypeofBoolean() { return this.type.toBeTypeofBoolean(); } public toBeTypeofBoolean() { return this.type.toBeTypeofBoolean(); }
public toBeTypeOf(typeName: string) { return this.type.toBeTypeOf(typeName); } public toBeTypeOf(typeName: string) { return this.type.toBeTypeOf(typeName); }
public toBeDefined() { return this.type.toBeDefined(); } public toBeDefined() { return this.type.toBeDefined(); }
// Namespaced matcher accessors // Namespaced matcher accessors
/** String-specific matchers */ /** String-specific matchers */
public get string() { public get string() {