Compare commits

..

10 Commits

Author SHA1 Message Date
af12f068aa 1.0.13 2022-03-14 13:07:47 +01:00
b5d2be3ab8 fix(core): update 2022-03-14 13:07:46 +01:00
6fdd61b230 1.0.12 2022-02-15 16:13:49 +01:00
cdcf082ee0 fix(core): update 2022-02-15 16:13:48 +01:00
52b8a3f0d1 1.0.11 2022-02-02 08:02:27 +01:00
9213e7905f fix(core): update 2022-02-02 08:02:26 +01:00
56fb434f7d 1.0.10 2022-02-02 04:26:22 +01:00
b147de4713 fix(core): update 2022-02-02 04:26:22 +01:00
6a43243a3b 1.0.9 2022-02-02 04:24:39 +01:00
6be0f9680b fix(core): update 2022-02-02 04:24:39 +01:00
4 changed files with 116 additions and 29 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@pushrocks/smartexpect", "name": "@pushrocks/smartexpect",
"version": "1.0.8", "version": "1.0.13",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@pushrocks/smartexpect", "name": "@pushrocks/smartexpect",
"version": "1.0.8", "version": "1.0.13",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@pushrocks/smartdelay": "^2.0.13", "@pushrocks/smartdelay": "^2.0.13",

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartexpect", "name": "@pushrocks/smartexpect",
"version": "1.0.8", "version": "1.0.13",
"private": false, "private": false,
"description": "manage expectations in code", "description": "manage expectations in code",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -36,4 +36,12 @@ tap.test('should be greater than', async () => {
smartexpect.expect(4).toBeLessThan(5); smartexpect.expect(4).toBeLessThan(5);
}) })
tap.test('should correctly determine toContain', async () => {
const hello = {
socool: 'yes'
};
const testArray = [hello];
smartexpect.expect(testArray).toContain(hello);
})
tap.start(); tap.start();

View File

@ -5,6 +5,7 @@ export type TExecutionType = 'sync' | 'async';
export class Assertion { export class Assertion {
executionMode: TExecutionType; executionMode: TExecutionType;
baseReference: any; baseReference: any;
propertyDrillDown: string[] = [];
private notSetting = false; private notSetting = false;
private timeoutSetting = 0; private timeoutSetting = 0;
constructor(baseReferenceArg: any, executionModeArg: TExecutionType) { constructor(baseReferenceArg: any, executionModeArg: TExecutionType) {
@ -12,6 +13,14 @@ export class Assertion {
this.executionMode = executionModeArg; this.executionMode = executionModeArg;
} }
private getObjectToTestReference() {
let returnObjectToTestReference = this.baseReference;
for (const property of this.propertyDrillDown) {
returnObjectToTestReference = returnObjectToTestReference[property];
}
return returnObjectToTestReference;
}
public get not() { public get not() {
this.notSetting = true; this.notSetting = true;
return this; return this;
@ -29,7 +38,7 @@ export class Assertion {
} else { } else {
let isOk = false; let isOk = false;
try { try {
runDirectOrNegated(checkFunction()) runDirectOrNegated(checkFunction());
} catch (e) { } catch (e) {
isOk = true; isOk = true;
} }
@ -37,7 +46,7 @@ export class Assertion {
throw new Error('Negated assertion is not ok!'); throw new Error('Negated assertion is not ok!');
} }
} }
} };
if (this.executionMode === 'async') { if (this.executionMode === 'async') {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
@ -47,14 +56,14 @@ export class Assertion {
if (this.timeoutSetting) { if (this.timeoutSetting) {
plugins.smartdelay.delayFor(this.timeoutSetting).then(() => { plugins.smartdelay.delayFor(this.timeoutSetting).then(() => {
if (done.status === 'pending') { if (done.status === 'pending') {
done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`)) done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`));
} }
}); });
} }
this.baseReference.then(promiseResultArg => { this.baseReference.then((promiseResultArg) => {
this.baseReference = promiseResultArg; this.baseReference = promiseResultArg;
done.resolve(runDirectOrNegated(checkFunction)); done.resolve(runDirectOrNegated(checkFunction));
}) });
} }
return done.promise; return done.promise;
} else { } else {
@ -64,9 +73,10 @@ export class Assertion {
public toBeTypeofString() { public toBeTypeofString() {
return this.runCheck(() => { return this.runCheck(() => {
if (typeof this.baseReference !== 'string') { if (typeof this.getObjectToTestReference() !== 'string') {
throw new Error( throw new Error(
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this.baseReference}` `Assertion failed: ${this.baseReference} with drill down ${this.propertyDrillDown} is not of type string, but typeof ${typeof this
.baseReference}`
); );
} }
}); });
@ -74,9 +84,10 @@ export class Assertion {
public toBeTypeofNumber() { public toBeTypeofNumber() {
return this.runCheck(() => { return this.runCheck(() => {
if (typeof this.baseReference !== 'number') { if (typeof this.getObjectToTestReference() !== 'number') {
throw new Error( throw new Error(
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this.baseReference}` `Assertion failed: ${this.baseReference} with drill down ${this.propertyDrillDown} is not of type string, but typeof ${typeof this
.baseReference}`
); );
} }
}); });
@ -84,9 +95,10 @@ export class Assertion {
public toBeTypeofBoolean() { public toBeTypeofBoolean() {
return this.runCheck(() => { return this.runCheck(() => {
if (typeof this.baseReference !== 'boolean') { if (typeof this.getObjectToTestReference() !== 'boolean') {
throw new Error( throw new Error(
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this.baseReference}` `Assertion failed: ${this.baseReference} with drill down ${this.propertyDrillDown} is not of type string, but typeof ${typeof this
.baseReference}`
); );
} }
}); });
@ -94,64 +106,131 @@ export class Assertion {
public toEqual(comparisonObject: any) { public toEqual(comparisonObject: any) {
return this.runCheck(() => { return this.runCheck(() => {
const result = plugins.fastDeepEqual(this.baseReference, comparisonObject); const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
if (!result) { if (!result) {
throw new Error(`${this.baseReference} does not equal ${comparisonObject}`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`);
} }
}); });
} }
public toBeTrue() { public toBeTrue() {
return this.runCheck(() => { return this.runCheck(() => {
const result = typeof this.baseReference === 'boolean' && this.baseReference === true; const result = typeof this.getObjectToTestReference() === 'boolean' && this.getObjectToTestReference() === true;
if (!result) { if (!result) {
throw new Error(`${this.baseReference} is not true or not of type boolean`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not true or not of type boolean`);
} }
}); });
} }
public toBeFalse() { public toBeFalse() {
return this.runCheck(() => { return this.runCheck(() => {
const result = typeof this.baseReference === 'boolean' && this.baseReference === false; const result = typeof this.getObjectToTestReference() === 'boolean' && this.getObjectToTestReference() === false;
if (!result) { if (!result) {
throw new Error(`${this.baseReference} is not false or not of type boolean`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not false or not of type boolean`);
} }
}); });
} }
public toBeInstanceOf(classArg: any) { public toBeInstanceOf(classArg: any) {
return this.runCheck(() => { return this.runCheck(() => {
const result = this.baseReference instanceof classArg; const result = this.getObjectToTestReference() instanceof classArg;
if (!result) { if (!result) {
throw new Error(`${this.baseReference} is not an instance of ${classArg}`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`);
} }
}); });
} }
public toHaveProperty(propertyArg: string) { public toHaveProperty(propertyArg: string) {
return this.runCheck(() => { return this.runCheck(() => {
const result = !!this.baseReference[propertyArg]; const result = !!this.getObjectToTestReference()[propertyArg];
if (!result) { if (!result) {
throw new Error(`${this.baseReference} does not have property ${propertyArg}`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} does not have property ${propertyArg}`);
} }
}); });
} }
public toBeGreaterThan(numberArg: number) { public toBeGreaterThan(numberArg: number) {
return this.runCheck(() => { return this.runCheck(() => {
const result = this.baseReference > numberArg; const result = this.getObjectToTestReference() > numberArg;
if (!result) { if (!result) {
throw new Error(`${this.baseReference} is not greater than ${numberArg}`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`);
} }
}); });
} }
public toBeLessThan(numberArg: number) { public toBeLessThan(numberArg: number) {
return this.runCheck(() => { return this.runCheck(() => {
const result = this.baseReference < numberArg; const result = this.getObjectToTestReference() < numberArg;
if (!result) { if (!result) {
throw new Error(`${this.baseReference} is not less than ${numberArg}`); throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not less than ${numberArg}`);
} }
}); });
} }
public toBeNull() {
return this.runCheck(() => {
const result = this.getObjectToTestReference() === null;
if (!result) {
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not null`);
}
});
}
public toBeUndefined() {
return this.runCheck(() => {
const result = this.getObjectToTestReference() === undefined;
if (!result) {
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not undefined`);
}
});
}
public toBeNullOrUndefined() {
return this.runCheck(() => {
const result = this.getObjectToTestReference() === null || this.getObjectToTestReference() === undefined;
if (!result) {
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not null or undefined`);
}
});
}
public toContain(itemArg: any) {
return this.runCheck(() => {
const result =
this.getObjectToTestReference() instanceof Array &&
this.getObjectToTestReference().includes(itemArg);
if (!result) {
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`);
}
});
}
public toStartWith(itemArg: any) {
return this.runCheck(() => {
const testObject = this.getObjectToTestReference();
const result =
typeof testObject === 'string' &&
testObject.startsWith(itemArg);
if (!result) {
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`);
}
});
}
public toEndWith(itemArg: any) {
return this.runCheck(() => {
const testObject = this.getObjectToTestReference();
const result =
typeof testObject === 'string' &&
testObject.endsWith(itemArg);
if (!result) {
throw new Error(`${this.baseReference} with drill down ${this.propertyDrillDown} is not contain ${itemArg}`);
}
});
}
public property(propertyNameArg: string) {
this.propertyDrillDown.push(propertyNameArg);
return this;
}
} }