Compare commits

...

4 Commits

5 changed files with 41 additions and 39 deletions

View File

@ -1,5 +1,18 @@
# 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)
Add new matchers and improve negation messaging

View File

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

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
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.'
}

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;
}
private runCheck(checkFunction: () => any) {
private runCheck(checkFunction: () => any): Assertion<T> | Promise<Assertion<T>> {
const runDirectOrNegated = (checkFunction: () => any) => {
if (!this.notSetting) {
return checkFunction();
@ -223,7 +223,7 @@ export class Assertion<T = unknown> {
};
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';
if (!isThenable) {
done.reject(new Error(`Expected a Promise but received: ${this.formatValue(this.baseReference)}`));
@ -241,41 +241,44 @@ export class Assertion<T = unknown> {
(res: any) => {
done.reject(new Error(`Expected Promise to reject but it resolved with ${this.formatValue(res)}`));
},
(err: any) => {
this.baseReference = err;
try {
const ret = runDirectOrNegated(checkFunction);
done.resolve(ret);
} catch (e: any) {
done.reject(e);
(err: any) => {
this.baseReference = err;
try {
runDirectOrNegated(checkFunction);
done.resolve(this);
} catch (e: any) {
done.reject(e);
}
}
}
);
} else {
(this.baseReference as Promise<any>).then(
(res: any) => {
this.baseReference = res;
try {
const ret = runDirectOrNegated(checkFunction);
done.resolve(ret);
} catch (e: any) {
done.reject(e);
}
},
(res: any) => {
this.baseReference = res;
try {
runDirectOrNegated(checkFunction);
done.resolve(this);
} catch (e: any) {
done.reject(e);
}
},
(err: any) => {
done.reject(err);
}
);
}
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(
assertionFunction: (value: any) => boolean,
errorMessage: string | ((value: any) => string)
) {
): Assertion<T> | Promise<Assertion<T>> {
// Prepare negation message based on the positive error template, if static
if (typeof errorMessage === 'string') {
this.negativeMessage = this.computeNegationMessage(errorMessage);
@ -359,6 +362,7 @@ export class Assertion<T = unknown> {
public toBeTypeofBoolean() { return this.type.toBeTypeofBoolean(); }
public toBeTypeOf(typeName: string) { return this.type.toBeTypeOf(typeName); }
public toBeDefined() { return this.type.toBeDefined(); }
// Namespaced matcher accessors
/** String-specific matchers */
public get string() {