2022-07-24 12:45:29 +02:00
|
|
|
import * as plugins from './smartexpect.plugins.js';
|
2022-01-21 03:33:24 +01:00
|
|
|
|
|
|
|
export type TExecutionType = 'sync' | 'async';
|
|
|
|
|
|
|
|
export class Assertion {
|
|
|
|
executionMode: TExecutionType;
|
|
|
|
baseReference: any;
|
2024-12-30 20:33:24 +01:00
|
|
|
propertyDrillDown: Array<string | number> = [];
|
|
|
|
|
2022-01-21 03:33:24 +01:00
|
|
|
private notSetting = false;
|
|
|
|
private timeoutSetting = 0;
|
2024-12-30 20:33:24 +01:00
|
|
|
private failMessage: string;
|
|
|
|
private successMessage: string;
|
|
|
|
|
2022-01-21 03:33:24 +01:00
|
|
|
constructor(baseReferenceArg: any, executionModeArg: TExecutionType) {
|
|
|
|
this.baseReference = baseReferenceArg;
|
|
|
|
this.executionMode = executionModeArg;
|
|
|
|
}
|
|
|
|
|
2022-02-15 16:13:48 +01:00
|
|
|
private getObjectToTestReference() {
|
|
|
|
let returnObjectToTestReference = this.baseReference;
|
|
|
|
for (const property of this.propertyDrillDown) {
|
2024-12-30 20:33:24 +01:00
|
|
|
if (returnObjectToTestReference == null) {
|
|
|
|
// if it's null or undefined, stop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We just directly access with bracket notation.
|
|
|
|
// If property is a string, it's like obj["someProp"];
|
|
|
|
// If property is a number, it's like obj[0].
|
2022-02-15 16:13:48 +01:00
|
|
|
returnObjectToTestReference = returnObjectToTestReference[property];
|
|
|
|
}
|
|
|
|
return returnObjectToTestReference;
|
|
|
|
}
|
|
|
|
|
2022-01-21 03:33:24 +01:00
|
|
|
public get not() {
|
|
|
|
this.notSetting = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public timeout(millisArg: number) {
|
|
|
|
this.timeoutSetting = millisArg;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-08-24 01:42:17 +02:00
|
|
|
public setFailMessage(failMessageArg: string) {
|
2024-08-24 01:36:23 +02:00
|
|
|
this.failMessage = failMessageArg;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-08-24 01:42:17 +02:00
|
|
|
public setSuccessMessage(successMessageArg: string) {
|
2024-08-24 01:36:23 +02:00
|
|
|
this.successMessage = successMessageArg;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-01-21 03:33:24 +01:00
|
|
|
private runCheck(checkFunction: () => any) {
|
|
|
|
const runDirectOrNegated = (checkFunction: () => any) => {
|
|
|
|
if (!this.notSetting) {
|
|
|
|
return checkFunction();
|
|
|
|
} else {
|
|
|
|
let isOk = false;
|
|
|
|
try {
|
2022-02-02 04:24:39 +01:00
|
|
|
runDirectOrNegated(checkFunction());
|
2022-01-21 03:33:24 +01:00
|
|
|
} catch (e) {
|
|
|
|
isOk = true;
|
|
|
|
}
|
|
|
|
if (!isOk) {
|
2024-08-24 01:36:23 +02:00
|
|
|
throw new Error(this.failMessage || 'Negated assertion is not ok!');
|
2022-01-21 03:33:24 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-02 04:24:39 +01:00
|
|
|
};
|
2022-01-21 03:33:24 +01:00
|
|
|
|
|
|
|
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.`));
|
|
|
|
} else {
|
|
|
|
if (this.timeoutSetting) {
|
|
|
|
plugins.smartdelay.delayFor(this.timeoutSetting).then(() => {
|
|
|
|
if (done.status === 'pending') {
|
2022-02-02 04:24:39 +01:00
|
|
|
done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`));
|
|
|
|
}
|
2022-01-21 03:33:24 +01:00
|
|
|
});
|
|
|
|
}
|
2024-12-30 20:33:24 +01:00
|
|
|
this.baseReference.then((promiseResultArg: any) => {
|
2022-01-21 03:33:24 +01:00
|
|
|
this.baseReference = promiseResultArg;
|
|
|
|
done.resolve(runDirectOrNegated(checkFunction));
|
2022-02-02 04:24:39 +01:00
|
|
|
});
|
2022-01-21 03:33:24 +01:00
|
|
|
}
|
|
|
|
return done.promise;
|
|
|
|
} else {
|
|
|
|
return runDirectOrNegated(checkFunction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 07:27:32 +02:00
|
|
|
public toBeDefined() {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
if (this.getObjectToTestReference() === undefined) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not defined`
|
2024-08-17 07:27:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-21 03:33:24 +01:00
|
|
|
public toBeTypeofString() {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
if (typeof this.getObjectToTestReference() !== 'string') {
|
2022-01-21 03:33:24 +01:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`Assertion failed: ${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} is not of type string, but typeof ${typeof this.baseReference}`
|
2022-01-21 03:33:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeTypeofNumber() {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
if (typeof this.getObjectToTestReference() !== 'number') {
|
2022-01-21 03:33:24 +01:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`Assertion failed: ${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} is not of type string, but typeof ${typeof this.baseReference}`
|
2022-01-21 03:33:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeTypeofBoolean() {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
if (typeof this.getObjectToTestReference() !== 'boolean') {
|
2022-01-21 03:33:24 +01:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`Assertion failed: ${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} is not of type string, but typeof ${typeof this.baseReference}`
|
2022-01-21 03:33:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-21 17:37:30 +01:00
|
|
|
|
|
|
|
public toEqual(comparisonObject: any) {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
|
2022-01-21 17:37:30 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-01-21 17:37:30 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-21 19:40:30 +01:00
|
|
|
|
2023-06-22 11:57:29 +02:00
|
|
|
public toMatch(comparisonObject: RegExp) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const result = comparisonObject.test(this.getObjectToTestReference());
|
|
|
|
if (!result) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not match regex ${comparisonObject}`
|
2023-06-22 11:57:29 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-21 19:40:30 +01:00
|
|
|
public toBeTrue() {
|
|
|
|
return this.runCheck(() => {
|
2022-07-24 12:45:29 +02:00
|
|
|
const result =
|
|
|
|
typeof this.getObjectToTestReference() === 'boolean' &&
|
|
|
|
this.getObjectToTestReference() === true;
|
2022-01-21 19:40:30 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-01-21 19:40:30 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeFalse() {
|
|
|
|
return this.runCheck(() => {
|
2022-07-24 12:45:29 +02:00
|
|
|
const result =
|
|
|
|
typeof this.getObjectToTestReference() === 'boolean' &&
|
|
|
|
this.getObjectToTestReference() === false;
|
2022-01-21 19:40:30 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-01-21 19:40:30 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeInstanceOf(classArg: any) {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
const result = this.getObjectToTestReference() instanceof classArg;
|
2022-01-21 19:40:30 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-01-21 19:40:30 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-02 02:45:45 +01:00
|
|
|
|
2022-07-24 12:45:29 +02:00
|
|
|
public toHaveProperty(propertyArg: string, equalsArg?: any) {
|
2022-02-02 02:45:45 +01:00
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const obj = this.getObjectToTestReference();
|
|
|
|
if (!obj || !(propertyArg in obj)) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not have property ${propertyArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
|
|
|
}
|
2024-12-30 20:33:24 +01:00
|
|
|
if (equalsArg !== undefined) {
|
|
|
|
if (obj[propertyArg] !== equalsArg) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does have property ${propertyArg}, but it does not equal ${equalsArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
|
|
|
}
|
2022-02-02 02:45:45 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-12 09:49:27 +02:00
|
|
|
public toHaveDeepProperty(properties: string[]) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
let obj = this.getObjectToTestReference();
|
|
|
|
let currentPath = '';
|
|
|
|
|
|
|
|
for (const property of properties) {
|
|
|
|
if (currentPath) {
|
|
|
|
currentPath += `.${property}`;
|
|
|
|
} else {
|
|
|
|
currentPath = property;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj || !(property in obj)) {
|
2024-12-30 20:33:24 +01:00
|
|
|
throw new Error(
|
|
|
|
this.failMessage ||
|
|
|
|
`Missing property at path "${currentPath}" in ${this.baseReference}`
|
|
|
|
);
|
2023-08-12 09:49:27 +02:00
|
|
|
}
|
|
|
|
obj = obj[property];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-02 02:45:45 +01:00
|
|
|
public toBeGreaterThan(numberArg: number) {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
const result = this.getObjectToTestReference() > numberArg;
|
2022-02-02 02:45:45 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-02-02 02:45:45 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeLessThan(numberArg: number) {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
const result = this.getObjectToTestReference() < numberArg;
|
2022-02-02 02:45:45 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-02-02 02:45:45 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-02 04:24:39 +01:00
|
|
|
|
|
|
|
public toBeNull() {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
const result = this.getObjectToTestReference() === null;
|
2022-02-02 04:24:39 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-02-02 04:24:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeUndefined() {
|
|
|
|
return this.runCheck(() => {
|
2022-02-15 16:13:48 +01:00
|
|
|
const result = this.getObjectToTestReference() === undefined;
|
2022-02-02 04:24:39 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-02-02 04:24:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeNullOrUndefined() {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const testRef = this.getObjectToTestReference();
|
|
|
|
const result = testRef === null || testRef === undefined;
|
2022-02-02 04:24:39 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-02-02 04:24:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-30 20:33:24 +01:00
|
|
|
// Array checks
|
2023-08-12 09:49:27 +02:00
|
|
|
|
2022-02-02 04:24:39 +01:00
|
|
|
public toContain(itemArg: any) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const testRef = this.getObjectToTestReference();
|
|
|
|
const result = Array.isArray(testRef) && testRef.includes(itemArg);
|
2022-02-02 04:24:39 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} does not contain ${itemArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-02-02 04:24:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-15 16:13:48 +01:00
|
|
|
|
2023-08-12 09:49:27 +02:00
|
|
|
public toBeEmptyArray() {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const arrayRef = this.getObjectToTestReference();
|
|
|
|
if (!Array.isArray(arrayRef) || arrayRef.length !== 0) {
|
2024-12-30 20:33:24 +01:00
|
|
|
throw new Error(
|
|
|
|
this.failMessage ||
|
|
|
|
`Expected ${this.baseReference} to be an empty array, but it was not.`
|
|
|
|
);
|
2023-08-12 09:49:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toContainAll(values: any[]) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const arrayRef = this.getObjectToTestReference();
|
|
|
|
if (!Array.isArray(arrayRef)) {
|
2024-12-30 20:33:24 +01:00
|
|
|
throw new Error(
|
|
|
|
this.failMessage ||
|
|
|
|
`Expected ${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} to be an array.`
|
|
|
|
);
|
2023-08-12 09:49:27 +02:00
|
|
|
}
|
|
|
|
for (const value of values) {
|
|
|
|
if (!arrayRef.includes(value)) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`Expected ${this.baseReference} to include value "${value}", but it did not.`
|
2023-08-12 09:49:27 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toExclude(value: any) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const arrayRef = this.getObjectToTestReference();
|
|
|
|
if (!Array.isArray(arrayRef)) {
|
2024-12-30 20:33:24 +01:00
|
|
|
throw new Error(
|
|
|
|
this.failMessage ||
|
|
|
|
`Expected ${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} to be an array.`
|
|
|
|
);
|
2023-08-12 09:49:27 +02:00
|
|
|
}
|
|
|
|
if (arrayRef.includes(value)) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`Expected ${this.baseReference} to exclude value "${value}", but it included it.`
|
2023-08-12 09:49:27 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:07:46 +01:00
|
|
|
public toStartWith(itemArg: any) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const testObject = this.getObjectToTestReference();
|
2022-07-24 12:45:29 +02:00
|
|
|
const result = typeof testObject === 'string' && testObject.startsWith(itemArg);
|
2022-03-14 13:07:46 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not start with ${itemArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-03-14 13:07:46 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toEndWith(itemArg: any) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const testObject = this.getObjectToTestReference();
|
2022-07-24 12:45:29 +02:00
|
|
|
const result = typeof testObject === 'string' && testObject.endsWith(itemArg);
|
2022-03-14 13:07:46 +01:00
|
|
|
if (!result) {
|
2022-07-24 12:45:29 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not end with ${itemArg}`
|
2022-07-24 12:45:29 +02:00
|
|
|
);
|
2022-03-14 13:07:46 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:08:50 +02:00
|
|
|
public toBeOneOf(values: any[]) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const result = values.includes(this.getObjectToTestReference());
|
|
|
|
if (!result) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not one of ${values}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toHaveLength(length: number) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const obj = this.getObjectToTestReference();
|
|
|
|
if (typeof obj.length !== 'number' || obj.length !== length) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not have a length of ${length}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeCloseTo(value: number, precision = 2) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const difference = Math.abs(this.getObjectToTestReference() - value);
|
|
|
|
if (difference > Math.pow(10, -precision) / 2) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} is not close to ${value} up to ${precision} decimal places`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toThrow(expectedError?: any) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
this.getObjectToTestReference()();
|
|
|
|
} catch (e) {
|
|
|
|
thrown = true;
|
|
|
|
if (expectedError && !(e instanceof expectedError)) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`Expected function to throw ${expectedError.name}, but it threw ${e.name}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!thrown) {
|
|
|
|
throw new Error(`Expected function to throw, but it didn't.`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeTruthy() {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
if (!this.getObjectToTestReference()) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not truthy`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeFalsy() {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
if (this.getObjectToTestReference()) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not falsy`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeGreaterThanOrEqual(numberArg: number) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
if (this.getObjectToTestReference() < numberArg) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} is not greater than or equal to ${numberArg}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeLessThanOrEqual(numberArg: number) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
if (this.getObjectToTestReference() > numberArg) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} is not less than or equal to ${numberArg}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toMatchObject(objectArg: object) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
// Implement a partial object match if needed.
|
|
|
|
const matchResult = plugins.fastDeepEqual(this.getObjectToTestReference(), objectArg);
|
|
|
|
if (!matchResult) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not match the object ${JSON.stringify(objectArg)}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toContainEqual(value: any) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const arr = this.getObjectToTestReference();
|
2024-12-30 20:33:24 +01:00
|
|
|
if (!Array.isArray(arr)) {
|
|
|
|
throw new Error(
|
|
|
|
this.failMessage || `Expected ${this.baseReference} to be an array but it is not.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const found = arr.some((item: any) => plugins.fastDeepEqual(item, value));
|
2023-08-11 18:08:50 +02:00
|
|
|
if (!found) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not contain the value ${JSON.stringify(value)}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeArray() {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
if (!Array.isArray(this.getObjectToTestReference())) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not an array`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toInclude(substring: string) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const testRef = this.getObjectToTestReference();
|
|
|
|
if (typeof testRef !== 'string' || !testRef.includes(substring)) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not include the substring ${substring}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:10:08 +02:00
|
|
|
public toHaveLengthGreaterThan(length: number) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const obj = this.getObjectToTestReference();
|
|
|
|
if (typeof obj.length !== 'number' || obj.length <= length) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not have a length greater than ${length}`
|
2023-08-11 18:10:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toHaveLengthLessThan(length: number) {
|
|
|
|
return this.runCheck(() => {
|
|
|
|
const obj = this.getObjectToTestReference();
|
|
|
|
if (typeof obj.length !== 'number' || obj.length >= length) {
|
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${
|
|
|
|
this.propertyDrillDown
|
|
|
|
} does not have a length less than ${length}`
|
2023-08-11 18:10:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:08:50 +02:00
|
|
|
public toBeDate() {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const testRef = this.getObjectToTestReference();
|
|
|
|
if (!(testRef instanceof Date)) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not a date`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeBeforeDate(date: Date) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const testRef = this.getObjectToTestReference();
|
|
|
|
if (!(testRef instanceof Date) || testRef >= date) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not before ${date}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toBeAfterDate(date: Date) {
|
|
|
|
return this.runCheck(() => {
|
2024-12-30 20:33:24 +01:00
|
|
|
const testRef = this.getObjectToTestReference();
|
|
|
|
if (!(testRef instanceof Date) || testRef <= date) {
|
2023-08-11 18:08:50 +02:00
|
|
|
throw new Error(
|
2024-12-30 20:33:24 +01:00
|
|
|
this.failMessage ||
|
|
|
|
`${this.baseReference} with drill down ${this.propertyDrillDown} is not after ${date}`
|
2023-08-11 18:08:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-30 20:33:24 +01:00
|
|
|
public customAssertion(
|
|
|
|
assertionFunction: (value: any) => boolean,
|
|
|
|
errorMessage: string
|
|
|
|
) {
|
2023-08-12 09:49:27 +02:00
|
|
|
return this.runCheck(() => {
|
|
|
|
if (!assertionFunction(this.getObjectToTestReference())) {
|
2024-08-24 01:36:23 +02:00
|
|
|
throw new Error(this.failMessage || errorMessage);
|
2023-08-12 09:49:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-30 20:33:24 +01:00
|
|
|
/**
|
|
|
|
* Drill into a property
|
|
|
|
*/
|
2022-02-15 16:13:48 +01:00
|
|
|
public property(propertyNameArg: string) {
|
|
|
|
this.propertyDrillDown.push(propertyNameArg);
|
|
|
|
return this;
|
|
|
|
}
|
2024-12-30 20:33:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Drill into an array index
|
|
|
|
*/
|
|
|
|
public arrayItem(indexArg: number) {
|
|
|
|
// Save the number (instead of "[index]")
|
|
|
|
this.propertyDrillDown.push(indexArg);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|