Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
d1be0d8ce2 | |||
3e5121171a | |||
af12f068aa | |||
b5d2be3ab8 | |||
6fdd61b230 | |||
cdcf082ee0 | |||
52b8a3f0d1 | |||
9213e7905f | |||
56fb434f7d | |||
b147de4713 | |||
6a43243a3b | |||
6be0f9680b | |||
57728989b7 | |||
9e92b8cff4 | |||
3bd650dc2c | |||
71cecfdc9a | |||
d3bdbf6260 | |||
402934c944 | |||
f7cab18412 | |||
6f5ceacad2 |
@ -18,17 +18,6 @@ before_script:
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
@ -100,10 +89,9 @@ codequality:
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
@ -123,11 +111,10 @@ trigger:
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci node install stable
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
|
21687
package-lock.json
generated
21687
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
@ -1,29 +1,31 @@
|
||||
{
|
||||
"name": "@pushrocks/smartexpect",
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.14",
|
||||
"private": false,
|
||||
"description": "manage expectations in code",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
"build": "(tsbuild --web)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.78",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.11.2",
|
||||
"@gitzone/tsbuild": "^2.1.63",
|
||||
"@gitzone/tsbundle": "^2.0.5",
|
||||
"@gitzone/tstest": "^1.0.71",
|
||||
"@pushrocks/tapbundle": "^5.0.3",
|
||||
"@types/node": "^18.0.6",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartpromise": "^3.1.6",
|
||||
"chai": "^4.3.4"
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"fast-deep-equal": "^3.1.3"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
|
29
test/test.ts
29
test/test.ts
@ -1,5 +1,5 @@
|
||||
import { tap } from '@pushrocks/tapbundle';
|
||||
import * as smartexpect from '../ts/index';
|
||||
import * as smartexpect from '../ts/index.js';
|
||||
|
||||
tap.test('sync tests', async () => {
|
||||
smartexpect.expect('hello').toBeTypeofString();
|
||||
@ -17,4 +17,31 @@ tap.test('async tests', async (toolsArg) => {
|
||||
await smartexpect.expectAsync(deferred.promise).not.toBeTypeofBoolean();
|
||||
});
|
||||
|
||||
tap.test('should check equality', async () => {
|
||||
smartexpect.expect('hithere').toEqual('hithere');
|
||||
smartexpect.expect('hithere').not.toEqual('hithere2');
|
||||
});
|
||||
|
||||
tap.test('should correctly state property presence', async () => {
|
||||
const testObject = {
|
||||
aprop: 'hello',
|
||||
};
|
||||
|
||||
smartexpect.expect(testObject).toHaveProperty('aprop');
|
||||
smartexpect.expect(testObject).not.toHaveProperty('aprop2');
|
||||
});
|
||||
|
||||
tap.test('should be greater than', async () => {
|
||||
smartexpect.expect(4).toBeGreaterThan(3);
|
||||
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();
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartexpect',
|
||||
version: '1.0.14',
|
||||
description: 'manage expectations in code'
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import * as plugins from './smartexpect.plugins';
|
||||
import * as plugins from './smartexpect.plugins.js';
|
||||
|
||||
import { Assertion } from './smartexpect.classes.assertion';
|
||||
import { Assertion } from './smartexpect.classes.assertion.js';
|
||||
|
||||
export const expect = (baseArg: any) => {
|
||||
const assertion = new Assertion(baseArg, 'sync');
|
||||
return assertion;
|
||||
}
|
||||
};
|
||||
|
||||
export const expectAsync = (baseArg: any) => {
|
||||
const assertion = new Assertion(baseArg, 'async');
|
||||
return assertion;
|
||||
}
|
||||
};
|
||||
|
@ -1,10 +1,11 @@
|
||||
import * as plugins from './smartexpect.plugins';
|
||||
import * as plugins from './smartexpect.plugins.js';
|
||||
|
||||
export type TExecutionType = 'sync' | 'async';
|
||||
|
||||
export class Assertion {
|
||||
executionMode: TExecutionType;
|
||||
baseReference: any;
|
||||
propertyDrillDown: string[] = [];
|
||||
private notSetting = false;
|
||||
private timeoutSetting = 0;
|
||||
constructor(baseReferenceArg: any, executionModeArg: TExecutionType) {
|
||||
@ -12,6 +13,14 @@ export class Assertion {
|
||||
this.executionMode = executionModeArg;
|
||||
}
|
||||
|
||||
private getObjectToTestReference() {
|
||||
let returnObjectToTestReference = this.baseReference;
|
||||
for (const property of this.propertyDrillDown) {
|
||||
returnObjectToTestReference = returnObjectToTestReference[property];
|
||||
}
|
||||
return returnObjectToTestReference;
|
||||
}
|
||||
|
||||
public get not() {
|
||||
this.notSetting = true;
|
||||
return this;
|
||||
@ -29,7 +38,7 @@ export class Assertion {
|
||||
} else {
|
||||
let isOk = false;
|
||||
try {
|
||||
runDirectOrNegated(checkFunction())
|
||||
runDirectOrNegated(checkFunction());
|
||||
} catch (e) {
|
||||
isOk = true;
|
||||
}
|
||||
@ -37,7 +46,7 @@ export class Assertion {
|
||||
throw new Error('Negated assertion is not ok!');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (this.executionMode === 'async') {
|
||||
const done = plugins.smartpromise.defer();
|
||||
@ -47,14 +56,14 @@ export class Assertion {
|
||||
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(`${this.baseReference} timed out at ${this.timeoutSetting}!`));
|
||||
}
|
||||
});
|
||||
}
|
||||
this.baseReference.then(promiseResultArg => {
|
||||
this.baseReference.then((promiseResultArg) => {
|
||||
this.baseReference = promiseResultArg;
|
||||
done.resolve(runDirectOrNegated(checkFunction));
|
||||
})
|
||||
});
|
||||
}
|
||||
return done.promise;
|
||||
} else {
|
||||
@ -64,9 +73,11 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofString() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.baseReference !== 'string') {
|
||||
if (typeof this.getObjectToTestReference() !== 'string') {
|
||||
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 +85,11 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofNumber() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.baseReference !== 'number') {
|
||||
if (typeof this.getObjectToTestReference() !== 'number') {
|
||||
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,11 +97,177 @@ export class Assertion {
|
||||
|
||||
public toBeTypeofBoolean() {
|
||||
return this.runCheck(() => {
|
||||
if (typeof this.baseReference !== 'boolean') {
|
||||
if (typeof this.getObjectToTestReference() !== 'boolean') {
|
||||
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}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toEqual(comparisonObject: any) {
|
||||
return this.runCheck(() => {
|
||||
const result = plugins.fastDeepEqual(this.getObjectToTestReference(), comparisonObject);
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} does not equal ${comparisonObject}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeTrue() {
|
||||
return this.runCheck(() => {
|
||||
const result =
|
||||
typeof this.getObjectToTestReference() === 'boolean' &&
|
||||
this.getObjectToTestReference() === true;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
`${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.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.baseReference} with drill down ${this.propertyDrillDown} is not an instance of ${classArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toHaveProperty(propertyArg: string, equalsArg?: any) {
|
||||
return this.runCheck(() => {
|
||||
const result = !!this.getObjectToTestReference()[propertyArg];
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
`${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}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeGreaterThan(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() > numberArg;
|
||||
if (!result) {
|
||||
throw new Error(
|
||||
`${this.baseReference} with drill down ${this.propertyDrillDown} is not greater than ${numberArg}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public toBeLessThan(numberArg: number) {
|
||||
return this.runCheck(() => {
|
||||
const result = this.getObjectToTestReference() < numberArg;
|
||||
if (!result) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,9 @@
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export {
|
||||
smartdelay,
|
||||
smartpromise
|
||||
}
|
||||
export { smartdelay, smartpromise };
|
||||
|
||||
// third party scope
|
||||
import { expect } from 'chai';
|
||||
import fastDeepEqual from 'fast-deep-equal';
|
||||
|
||||
export {
|
||||
expect
|
||||
}
|
||||
export { fastDeepEqual };
|
||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Reference in New Issue
Block a user