feat(assertion): Enhanced the assertion error messaging and added new test cases.
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartexpect',
|
||||
version: '1.5.0',
|
||||
version: '1.6.0',
|
||||
description: 'A testing library to manage expectations in code, offering both synchronous and asynchronous assertion methods.'
|
||||
}
|
||||
|
@ -33,6 +33,57 @@ export class Assertion {
|
||||
return returnObjectToTestReference;
|
||||
}
|
||||
|
||||
private formatDrillDown(): string {
|
||||
if (!this.propertyDrillDown || this.propertyDrillDown.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const path = this.propertyDrillDown.map(prop => {
|
||||
if (typeof prop === 'number') {
|
||||
return `[${prop}]`;
|
||||
} else {
|
||||
return `.${prop}`;
|
||||
}
|
||||
}).join('');
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
private formatValue(value: any): string {
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
} else if (value === undefined) {
|
||||
return 'undefined';
|
||||
} else if (typeof value === 'object') {
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch (e) {
|
||||
return `[Object ${value.constructor.name}]`;
|
||||
}
|
||||
} else if (typeof value === 'function') {
|
||||
return `[Function${value.name ? ': ' + value.name : ''}]`;
|
||||
} else if (typeof value === 'string') {
|
||||
return `"${value}"`;
|
||||
} else {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
private createErrorMessage(message: string): string {
|
||||
if (this.failMessage) {
|
||||
return this.failMessage;
|
||||
}
|
||||
|
||||
const testValue = this.getObjectToTestReference();
|
||||
const formattedValue = this.formatValue(testValue);
|
||||
const drillDown = this.formatDrillDown();
|
||||
|
||||
// Replace placeholders in the message
|
||||
return message
|
||||
.replace('{value}', formattedValue)
|
||||
.replace('{path}', drillDown || '');
|
||||
}
|
||||
|
||||
public get not() {
|
||||
this.notSetting = true;
|
||||
return this;
|
||||
@ -65,7 +116,7 @@ export class Assertion {
|
||||
isOk = true;
|
||||
}
|
||||
if (!isOk) {
|
||||
throw new Error(this.failMessage || 'Negated assertion is not ok!');
|
||||
throw new Error(this.failMessage || 'Negated assertion failed');
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -73,12 +124,12 @@ export class Assertion {
|
||||
if (this.executionMode === 'async') {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (!(this.baseReference instanceof Promise)) {
|
||||
done.reject(new Error(`${this.baseReference} is not of type promise.`));
|
||||
done.reject(new Error(`Expected a Promise but received: ${this.formatValue(this.baseReference)}`));
|
||||
} else {
|
||||
if (this.timeoutSetting) {
|
||||
plugins.smartdelay.delayFor(this.timeoutSetting).then(() => {
|
||||
if (done.status === 'pending') {
|
||||
done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`));
|
||||
done.reject(new Error(`Promise timed out after ${this.timeoutSetting}ms`));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -97,8 +148,7 @@ export class Assertion {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() === undefined) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not defined`
|
||||
this.createErrorMessage('Expected value{path} to be defined, but got undefined')
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -106,12 +156,10 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofString() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.getObjectToTestReference() !== 'string') {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
this.createErrorMessage(`Expected value{path} to be of type string, but got ${typeof value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -119,12 +167,10 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofNumber() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.getObjectToTestReference() !== 'number') {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'number') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
this.createErrorMessage(`Expected value{path} to be of type number, but got ${typeof value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -132,78 +178,10 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofBoolean() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.getObjectToTestReference() !== 'boolean') {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'boolean') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type string, but typeof ${typeof this.baseReference}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toEqual(comparisonObject: any) {
|
||||
return this.runCheck(() => {
|
||||
const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toMatch(comparisonObject: RegExp) {
|
||||
return this.runCheck(() => {
|
||||
const result = comparisonObject.test(this.getObjectToTestReference());
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not match regex ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTrue() {
|
||||
return this.runCheck(() => {
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === true;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeFalse() {
|
||||
return this.runCheck(() => {
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === false;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeInstanceOf(classArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() instanceof classArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
|
||||
this.createErrorMessage(`Expected value{path} to be of type boolean, but got ${typeof value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -211,13 +189,71 @@ export class Assertion {
|
||||
|
||||
public toBeTypeOf(expectedType: string) {
|
||||
return this.runCheck(() => {
|
||||
const actualType = typeof this.getObjectToTestReference();
|
||||
const value = this.getObjectToTestReference();
|
||||
const actualType = typeof value;
|
||||
if (actualType !== expectedType) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Assertion failed: ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not of type ${expectedType}, but typeof ${actualType}`
|
||||
this.createErrorMessage(`Expected value{path} to be of type ${expectedType}, but got ${actualType}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toEqual(comparisonObject: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = plugins.fastDeepEqual(value, comparisonObject);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to equal ${this.formatValue(comparisonObject)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toMatch(comparisonObject: RegExp) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = comparisonObject.test(value);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to match regex ${comparisonObject}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTrue() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'boolean' && value === true;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be true, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeFalse() {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'boolean' && value === false;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be false, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeInstanceOf(classArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value instanceof classArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be an instance of ${classArg.name || 'provided class'}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -228,19 +264,15 @@ export class Assertion {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (!obj || !(propertyArg in obj)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have property ${propertyArg}`
|
||||
this.createErrorMessage(`Expected value{path} to have property '${propertyArg}'`)
|
||||
);
|
||||
}
|
||||
if (equalsArg !== undefined) {
|
||||
if (obj[propertyArg] !== equalsArg) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does have property ${propertyArg}, but it does not equal ${equalsArg}`
|
||||
this.createErrorMessage(
|
||||
`Expected property '${propertyArg}' of value{path} to equal ${this.formatValue(equalsArg)}, but got ${this.formatValue(obj[propertyArg])}`
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -261,8 +293,7 @@ export class Assertion {
|
||||
|
||||
if (!obj || !(property in obj)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Missing property at path "${currentPath}" in ${this.baseReference}`
|
||||
this.createErrorMessage(`Expected value{path} to have property at path '${currentPath}'`)
|
||||
);
|
||||
}
|
||||
obj = obj[property];
|
||||
@ -272,11 +303,11 @@ export class Assertion {
|
||||
|
||||
public toBeGreaterThan(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() > numberArg;
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value > numberArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
|
||||
this.createErrorMessage(`Expected value{path} to be greater than ${numberArg}, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -284,11 +315,11 @@ export class Assertion {
|
||||
|
||||
public toBeLessThan(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() < numberArg;
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value < numberArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`
|
||||
this.createErrorMessage(`Expected value{path} to be less than ${numberArg}, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -296,11 +327,11 @@ export class Assertion {
|
||||
|
||||
public toBeNull() {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() === null;
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value === null;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`
|
||||
this.createErrorMessage(`Expected value{path} to be null, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -308,11 +339,11 @@ export class Assertion {
|
||||
|
||||
public toBeUndefined() {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() === undefined;
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value === undefined;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`
|
||||
this.createErrorMessage(`Expected value{path} to be undefined, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -320,12 +351,11 @@ export class Assertion {
|
||||
|
||||
public toBeNullOrUndefined() {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
const result = testRef === null || testRef === undefined;
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = value === null || value === undefined;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`
|
||||
this.createErrorMessage(`Expected value{path} to be null or undefined, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -335,12 +365,11 @@ export class Assertion {
|
||||
|
||||
public toContain(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
const result = Array.isArray(testRef) && testRef.includes(itemArg);
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = Array.isArray(value) && value.includes(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not contain ${itemArg}`
|
||||
this.createErrorMessage(`Expected array{path} to contain ${this.formatValue(itemArg)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -348,11 +377,15 @@ export class Assertion {
|
||||
|
||||
public toBeEmptyArray() {
|
||||
return this.runCheck(() => {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef) || arrayRef.length !== 0) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to be an empty array, but it was not.`
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof value}`)
|
||||
);
|
||||
}
|
||||
if (value.length !== 0) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected array{path} to be empty, but it has ${value.length} elements`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -360,41 +393,33 @@ export class Assertion {
|
||||
|
||||
public toContainAll(values: any[]) {
|
||||
return this.runCheck(() => {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef)) {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} to be an array.`
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
|
||||
);
|
||||
}
|
||||
for (const value of values) {
|
||||
if (!arrayRef.includes(value)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to include value "${value}", but it did not.`
|
||||
);
|
||||
}
|
||||
|
||||
const missing = values.filter(v => !arr.includes(v));
|
||||
if (missing.length > 0) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected array{path} to contain all values ${this.formatValue(values)}, but missing: ${this.formatValue(missing)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toExclude(value: any) {
|
||||
return this.runCheck(() => {
|
||||
const arrayRef = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arrayRef)) {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} to be an array.`
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
|
||||
);
|
||||
}
|
||||
if (arrayRef.includes(value)) {
|
||||
if (arr.includes(value)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected ${this.baseReference} to exclude value "${value}", but it included it.`
|
||||
this.createErrorMessage(`Expected array{path} to exclude ${this.formatValue(value)}, but it was found`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -402,14 +427,11 @@ export class Assertion {
|
||||
|
||||
public toStartWith(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const testObject = this.getObjectToTestReference();
|
||||
const result = typeof testObject === 'string' && testObject.startsWith(itemArg);
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'string' && value.startsWith(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not start with ${itemArg}`
|
||||
this.createErrorMessage(`Expected string{path} to start with "${itemArg}", but got "${value}"`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -417,14 +439,11 @@ export class Assertion {
|
||||
|
||||
public toEndWith(itemArg: any) {
|
||||
return this.runCheck(() => {
|
||||
const testObject = this.getObjectToTestReference();
|
||||
const result = typeof testObject === 'string' && testObject.endsWith(itemArg);
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = typeof value === 'string' && value.endsWith(itemArg);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not end with ${itemArg}`
|
||||
this.createErrorMessage(`Expected string{path} to end with "${itemArg}", but got "${value}"`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -432,11 +451,11 @@ export class Assertion {
|
||||
|
||||
public toBeOneOf(values: any[]) {
|
||||
return this.runCheck(() => {
|
||||
const result = values.includes(this.getObjectToTestReference());
|
||||
const value = this.getObjectToTestReference();
|
||||
const result = values.includes(value);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not one of ${values}`
|
||||
this.createErrorMessage(`Expected value{path} to be one of ${this.formatValue(values)}, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -445,12 +464,14 @@ export class Assertion {
|
||||
public toHaveLength(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number' || obj.length !== length) {
|
||||
if (typeof obj.length !== 'number') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length of ${length}`
|
||||
this.createErrorMessage(`Expected value{path} to have a length property, but it doesn't`)
|
||||
);
|
||||
}
|
||||
if (obj.length !== length) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to have length ${length}, but got length ${obj.length}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -458,13 +479,12 @@ export class Assertion {
|
||||
|
||||
public toBeCloseTo(value: number, precision = 2) {
|
||||
return this.runCheck(() => {
|
||||
const difference = Math.abs(this.getObjectToTestReference() - value);
|
||||
if (difference > Math.pow(10, -precision) / 2) {
|
||||
const actual = this.getObjectToTestReference();
|
||||
const difference = Math.abs(actual - value);
|
||||
const epsilon = Math.pow(10, -precision) / 2;
|
||||
if (difference > epsilon) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not close to ${value} up to ${precision} decimal places`
|
||||
this.createErrorMessage(`Expected value{path} to be close to ${value} (within ${epsilon}), but the difference was ${difference}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -472,30 +492,42 @@ export class Assertion {
|
||||
|
||||
public toThrow(expectedError?: any) {
|
||||
return this.runCheck(() => {
|
||||
const fn = this.getObjectToTestReference();
|
||||
if (typeof fn !== 'function') {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to be a function, but got ${typeof fn}`)
|
||||
);
|
||||
}
|
||||
|
||||
let thrown = false;
|
||||
let error: any;
|
||||
|
||||
try {
|
||||
this.getObjectToTestReference()();
|
||||
fn();
|
||||
} catch (e) {
|
||||
thrown = true;
|
||||
error = e;
|
||||
if (expectedError && !(e instanceof expectedError)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`Expected function to throw ${expectedError.name}, but it threw ${e.name}`
|
||||
this.createErrorMessage(`Expected function{path} to throw ${expectedError.name}, but it threw ${e.constructor.name}`)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!thrown) {
|
||||
throw new Error(`Expected function to throw, but it didn't.`);
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected function{path} to throw, but it didn't throw any error`)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTruthy() {
|
||||
return this.runCheck(() => {
|
||||
if (!this.getObjectToTestReference()) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!value) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not truthy`
|
||||
this.createErrorMessage(`Expected value{path} to be truthy, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -503,10 +535,10 @@ export class Assertion {
|
||||
|
||||
public toBeFalsy() {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference()) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (value) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not falsy`
|
||||
this.createErrorMessage(`Expected value{path} to be falsy, but got ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -514,12 +546,10 @@ export class Assertion {
|
||||
|
||||
public toBeGreaterThanOrEqual(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() < numberArg) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (value < numberArg) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not greater than or equal to ${numberArg}`
|
||||
this.createErrorMessage(`Expected value{path} to be greater than or equal to ${numberArg}, but got ${value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -527,12 +557,10 @@ export class Assertion {
|
||||
|
||||
public toBeLessThanOrEqual(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
if (this.getObjectToTestReference() > numberArg) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (value > numberArg) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} is not less than or equal to ${numberArg}`
|
||||
this.createErrorMessage(`Expected value{path} to be less than or equal to ${numberArg}, but got ${value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -540,14 +568,11 @@ export class Assertion {
|
||||
|
||||
public toMatchObject(objectArg: object) {
|
||||
return this.runCheck(() => {
|
||||
// Implement a partial object match if needed.
|
||||
const matchResult = plugins.fastDeepEqual(this.getObjectToTestReference(), objectArg);
|
||||
const value = this.getObjectToTestReference();
|
||||
const matchResult = plugins.fastDeepEqual(value, objectArg);
|
||||
if (!matchResult) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not match the object ${JSON.stringify(objectArg)}`
|
||||
this.createErrorMessage(`Expected value{path} to match ${this.formatValue(objectArg)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -558,16 +583,14 @@ export class Assertion {
|
||||
const arr = this.getObjectToTestReference();
|
||||
if (!Array.isArray(arr)) {
|
||||
throw new Error(
|
||||
this.failMessage || `Expected ${this.baseReference} to be an array but it is not.`
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof arr}`)
|
||||
);
|
||||
}
|
||||
|
||||
const found = arr.some((item: any) => plugins.fastDeepEqual(item, value));
|
||||
if (!found) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not contain the value ${JSON.stringify(value)}`
|
||||
this.createErrorMessage(`Expected array{path} to contain an item equal to ${this.formatValue(value)}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -575,10 +598,10 @@ export class Assertion {
|
||||
|
||||
public toBeArray() {
|
||||
return this.runCheck(() => {
|
||||
if (!Array.isArray(this.getObjectToTestReference())) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an array`
|
||||
this.createErrorMessage(`Expected value{path} to be an array, but got ${typeof value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -586,13 +609,15 @@ export class Assertion {
|
||||
|
||||
public toInclude(substring: string) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (typeof testRef !== 'string' || !testRef.includes(substring)) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not include the substring ${substring}`
|
||||
this.createErrorMessage(`Expected value{path} to be a string, but got ${typeof value}`)
|
||||
);
|
||||
}
|
||||
if (!value.includes(substring)) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected string{path} to include "${substring}", but it doesn't`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -601,12 +626,14 @@ export class Assertion {
|
||||
public toHaveLengthGreaterThan(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number' || obj.length <= length) {
|
||||
if (typeof obj.length !== 'number') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length greater than ${length}`
|
||||
this.createErrorMessage(`Expected value{path} to have a length property, but it doesn't`)
|
||||
);
|
||||
}
|
||||
if (obj.length <= length) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to have length greater than ${length}, but got length ${obj.length}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -615,12 +642,14 @@ export class Assertion {
|
||||
public toHaveLengthLessThan(length: number) {
|
||||
return this.runCheck(() => {
|
||||
const obj = this.getObjectToTestReference();
|
||||
if (typeof obj.length !== 'number' || obj.length >= length) {
|
||||
if (typeof obj.length !== 'number') {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${
|
||||
this.propertyDrillDown
|
||||
} does not have a length less than ${length}`
|
||||
this.createErrorMessage(`Expected value{path} to have a length property, but it doesn't`)
|
||||
);
|
||||
}
|
||||
if (obj.length >= length) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected value{path} to have length less than ${length}, but got length ${obj.length}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -628,11 +657,10 @@ export class Assertion {
|
||||
|
||||
public toBeDate() {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date)) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!(value instanceof Date)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not a date`
|
||||
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -640,11 +668,15 @@ export class Assertion {
|
||||
|
||||
public toBeBeforeDate(date: Date) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date) || testRef >= date) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!(value instanceof Date)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not before ${date}`
|
||||
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
|
||||
);
|
||||
}
|
||||
if (value >= date) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected date{path} to be before ${date.toISOString()}, but got ${value.toISOString()}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -652,11 +684,15 @@ export class Assertion {
|
||||
|
||||
public toBeAfterDate(date: Date) {
|
||||
return this.runCheck(() => {
|
||||
const testRef = this.getObjectToTestReference();
|
||||
if (!(testRef instanceof Date) || testRef <= date) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!(value instanceof Date)) {
|
||||
throw new Error(
|
||||
this.failMessage ||
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not after ${date}`
|
||||
this.createErrorMessage(`Expected value{path} to be a Date, but got ${value.constructor ? value.constructor.name : typeof value}`)
|
||||
);
|
||||
}
|
||||
if (value <= date) {
|
||||
throw new Error(
|
||||
this.createErrorMessage(`Expected date{path} to be after ${date.toISOString()}, but got ${value.toISOString()}`)
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -667,7 +703,8 @@ export class Assertion {
|
||||
errorMessage: string
|
||||
) {
|
||||
return this.runCheck(() => {
|
||||
if (!assertionFunction(this.getObjectToTestReference())) {
|
||||
const value = this.getObjectToTestReference();
|
||||
if (!assertionFunction(value)) {
|
||||
throw new Error(this.failMessage || errorMessage);
|
||||
}
|
||||
});
|
||||
@ -691,8 +728,9 @@ export class Assertion {
|
||||
}
|
||||
|
||||
public log() {
|
||||
console.log(`this is the object to test:`);
|
||||
console.log(`Current value:`);
|
||||
console.log(JSON.stringify(this.getObjectToTestReference(), null, 2));
|
||||
console.log(`Path: ${this.formatDrillDown() || '(root)'}`);
|
||||
return this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user