feat(assertions): Add custom fail and success messages for assertions

This commit is contained in:
Philipp Kunz 2024-08-24 01:36:23 +02:00
parent 04e3c1a9ac
commit f83c64e1d4
4 changed files with 63 additions and 45 deletions

View File

@ -1,5 +1,12 @@
# Changelog
## 2024-08-24 - 1.2.0 - feat(assertions)
Add custom fail and success messages for assertions
- Implemented withFailMessage method in Assertion class to customize fail messages
- Implemented withSuccessMessage method in Assertion class to customize success messages
- Enhanced error messages to use custom fail messages when provided
## 2024-08-17 - 1.1.0 - feat(assertion)
Add toBeDefined assertion method

View File

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

View File

@ -1,5 +1,3 @@
import * as plugins from './smartexpect.plugins.js';
import { Assertion } from './smartexpect.classes.assertion.js';
export const expect = (baseArg: any) => {
@ -11,3 +9,4 @@ export const expectAsync = (baseArg: any) => {
const assertion = new Assertion(baseArg, 'async');
return assertion;
};

View File

@ -31,6 +31,18 @@ export class Assertion {
return this;
}
private failMessage: string;
public withFailMessage(failMessageArg: string) {
this.failMessage = failMessageArg;
return this;
}
private successMessage: string;
public withSuccessMessage(successMessageArg: string) {
this.successMessage = successMessageArg;
return this;
}
private runCheck(checkFunction: () => any) {
const runDirectOrNegated = (checkFunction: () => any) => {
if (!this.notSetting) {
@ -43,7 +55,7 @@ export class Assertion {
isOk = true;
}
if (!isOk) {
throw new Error('Negated assertion is not ok!');
throw new Error(this.failMessage || 'Negated assertion is not ok!');
}
}
};
@ -78,7 +90,7 @@ export class Assertion {
return this.runCheck(() => {
if (this.getObjectToTestReference() === undefined) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not defined`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not defined`
);
}
});
@ -91,7 +103,7 @@ export class Assertion {
return this.runCheck(() => {
if (typeof this.getObjectToTestReference() !== 'string') {
throw new Error(
`Assertion failed: ${this.baseReference} with drill down ${
this.failMessage || `Assertion failed: ${this.baseReference} with drill down ${
this.propertyDrillDown
} is not of type string, but typeof ${typeof this.baseReference}`
);
@ -103,7 +115,7 @@ export class Assertion {
return this.runCheck(() => {
if (typeof this.getObjectToTestReference() !== 'number') {
throw new Error(
`Assertion failed: ${this.baseReference} with drill down ${
this.failMessage || `Assertion failed: ${this.baseReference} with drill down ${
this.propertyDrillDown
} is not of type string, but typeof ${typeof this.baseReference}`
);
@ -115,7 +127,7 @@ export class Assertion {
return this.runCheck(() => {
if (typeof this.getObjectToTestReference() !== 'boolean') {
throw new Error(
`Assertion failed: ${this.baseReference} with drill down ${
this.failMessage || `Assertion failed: ${this.baseReference} with drill down ${
this.propertyDrillDown
} is not of type string, but typeof ${typeof this.baseReference}`
);
@ -128,7 +140,7 @@ export class Assertion {
const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
);
}
});
@ -139,7 +151,7 @@ export class Assertion {
const result = comparisonObject.test(this.getObjectToTestReference());
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
);
}
});
@ -152,7 +164,7 @@ export class Assertion {
this.getObjectToTestReference() === true;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`
);
}
});
@ -165,7 +177,7 @@ export class Assertion {
this.getObjectToTestReference() === false;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`
);
}
});
@ -176,7 +188,7 @@ export class Assertion {
const result = this.getObjectToTestReference() instanceof classArg;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
);
}
});
@ -187,13 +199,13 @@ export class Assertion {
const result = !!this.getObjectToTestReference()[propertyArg];
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not have property ${propertyArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not have property ${propertyArg}`
);
}
if (equalsArg) {
if (result !== equalsArg) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does have property ${propertyArg}, but it does not equal ${equalsArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does have property ${propertyArg}, but it does not equal ${equalsArg}`
);
}
}
@ -213,7 +225,7 @@ export class Assertion {
}
if (!obj || !(property in obj)) {
throw new Error(`Missing property at path "${currentPath}" in ${this.baseReference}`);
throw new Error(this.failMessage || `Missing property at path "${currentPath}" in ${this.baseReference}`);
}
obj = obj[property];
}
@ -225,7 +237,7 @@ export class Assertion {
const result = this.getObjectToTestReference() > numberArg;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
);
}
});
@ -236,7 +248,7 @@ export class Assertion {
const result = this.getObjectToTestReference() < numberArg;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`
);
}
});
@ -247,7 +259,7 @@ export class Assertion {
const result = this.getObjectToTestReference() === null;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not null`
);
}
});
@ -258,7 +270,7 @@ export class Assertion {
const result = this.getObjectToTestReference() === undefined;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`
);
}
});
@ -270,7 +282,7 @@ export class Assertion {
this.getObjectToTestReference() === null || this.getObjectToTestReference() === undefined;
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`
);
}
});
@ -285,7 +297,7 @@ export class Assertion {
this.getObjectToTestReference().includes(itemArg);
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`
);
}
});
@ -295,7 +307,7 @@ export class Assertion {
return this.runCheck(() => {
const arrayRef = this.getObjectToTestReference();
if (!Array.isArray(arrayRef) || arrayRef.length !== 0) {
throw new Error(`Expected ${this.baseReference} to be an empty array, but it was not.`);
throw new Error(this.failMessage || `Expected ${this.baseReference} to be an empty array, but it was not.`);
}
});
}
@ -304,13 +316,13 @@ export class Assertion {
return this.runCheck(() => {
const arrayRef = this.getObjectToTestReference();
if (!Array.isArray(arrayRef)) {
throw new Error(`Expected ${this.baseReference} to be an array.`);
throw new Error(this.failMessage || `Expected ${this.baseReference} to be an array.`);
}
for (const value of values) {
if (!arrayRef.includes(value)) {
throw new Error(
`Expected ${this.baseReference} to include value "${value}", but it did not.`
this.failMessage || `Expected ${this.baseReference} to include value "${value}", but it did not.`
);
}
}
@ -321,12 +333,12 @@ export class Assertion {
return this.runCheck(() => {
const arrayRef = this.getObjectToTestReference();
if (!Array.isArray(arrayRef)) {
throw new Error(`Expected ${this.baseReference} to be an array.`);
throw new Error(this.failMessage || `Expected ${this.baseReference} to be an array.`);
}
if (arrayRef.includes(value)) {
throw new Error(
`Expected ${this.baseReference} to exclude value "${value}", but it included it.`
this.failMessage || `Expected ${this.baseReference} to exclude value "${value}", but it included it.`
);
}
});
@ -338,7 +350,7 @@ export class Assertion {
const result = typeof testObject === 'string' && testObject.startsWith(itemArg);
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`
);
}
});
@ -350,7 +362,7 @@ export class Assertion {
const result = typeof testObject === 'string' && testObject.endsWith(itemArg);
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`
);
}
});
@ -363,7 +375,7 @@ export class Assertion {
const result = values.includes(this.getObjectToTestReference());
if (!result) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not one of ${values}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not one of ${values}`
);
}
});
@ -374,7 +386,7 @@ export class Assertion {
const obj = this.getObjectToTestReference();
if (typeof obj.length !== 'number' || obj.length !== length) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not have a length of ${length}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not have a length of ${length}`
);
}
});
@ -385,7 +397,7 @@ export class Assertion {
const difference = Math.abs(this.getObjectToTestReference() - value);
if (difference > Math.pow(10, -precision) / 2) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not close to ${value} up to ${precision} decimal places`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not close to ${value} up to ${precision} decimal places`
);
}
});
@ -400,7 +412,7 @@ export class Assertion {
thrown = true;
if (expectedError && !(e instanceof expectedError)) {
throw new Error(
`Expected function to throw ${expectedError.name}, but it threw ${e.name}`
this.failMessage || `Expected function to throw ${expectedError.name}, but it threw ${e.name}`
);
}
}
@ -414,7 +426,7 @@ export class Assertion {
return this.runCheck(() => {
if (!this.getObjectToTestReference()) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not truthy`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not truthy`
);
}
});
@ -424,7 +436,7 @@ export class Assertion {
return this.runCheck(() => {
if (this.getObjectToTestReference()) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not falsy`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not falsy`
);
}
});
@ -434,7 +446,7 @@ export class Assertion {
return this.runCheck(() => {
if (this.getObjectToTestReference() <= numberArg) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than or equal to ${numberArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than or equal to ${numberArg}`
);
}
});
@ -455,7 +467,7 @@ export class Assertion {
const partialMatch = plugins.fastDeepEqual(this.getObjectToTestReference(), objectArg); // Note: Implement a deep comparison function or use one from a library
if (!partialMatch) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not match the object ${objectArg}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not match the object ${objectArg}`
);
}
});
@ -477,7 +489,7 @@ export class Assertion {
return this.runCheck(() => {
if (!Array.isArray(this.getObjectToTestReference())) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an array`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not an array`
);
}
});
@ -487,7 +499,7 @@ export class Assertion {
return this.runCheck(() => {
if (!this.getObjectToTestReference().includes(substring)) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not include the substring ${substring}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not include the substring ${substring}`
);
}
});
@ -498,7 +510,7 @@ export class Assertion {
const obj = this.getObjectToTestReference();
if (typeof obj.length !== 'number' || obj.length <= length) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not have a length greater than ${length}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not have a length greater than ${length}`
);
}
});
@ -509,7 +521,7 @@ export class Assertion {
const obj = this.getObjectToTestReference();
if (typeof obj.length !== 'number' || obj.length >= length) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} does not have a length less than ${length}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} does not have a length less than ${length}`
);
}
});
@ -519,7 +531,7 @@ export class Assertion {
return this.runCheck(() => {
if (!(this.getObjectToTestReference() instanceof Date)) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not a date`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not a date`
);
}
});
@ -529,7 +541,7 @@ export class Assertion {
return this.runCheck(() => {
if (!(this.getObjectToTestReference() < date)) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not before ${date}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not before ${date}`
);
}
});
@ -539,7 +551,7 @@ export class Assertion {
return this.runCheck(() => {
if (!(this.getObjectToTestReference() > date)) {
throw new Error(
`${this.baseReference} with drill down ${this.propertyDrillDown} is not after ${date}`
this.failMessage || `${this.baseReference} with drill down ${this.propertyDrillDown} is not after ${date}`
);
}
});
@ -548,7 +560,7 @@ export class Assertion {
public customAssertion(assertionFunction: (value: any) => boolean, errorMessage: string) {
return this.runCheck(() => {
if (!assertionFunction(this.getObjectToTestReference())) {
throw new Error(errorMessage);
throw new Error(this.failMessage || errorMessage);
}
});
}