Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
d3bdbf6260 | |||
402934c944 | |||
f7cab18412 | |||
6f5ceacad2 | |||
998827f1bf | |||
14ab9ee908 | |||
1fbc9b6001 |
19
package-lock.json
generated
19
package-lock.json
generated
@ -1,13 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartexpect",
|
"name": "@pushrocks/smartexpect",
|
||||||
"version": "1.0.2",
|
"version": "1.0.6",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@pushrocks/smartexpect",
|
"name": "@pushrocks/smartexpect",
|
||||||
"version": "1.0.2",
|
"version": "1.0.6",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
|
"fast-deep-equal": "^3.1.3"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@gitzone/tsbundle": "^1.0.78",
|
"@gitzone/tsbundle": "^1.0.78",
|
||||||
@ -2228,7 +2233,6 @@
|
|||||||
"version": "2.0.13",
|
"version": "2.0.13",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.13.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.13.tgz",
|
||||||
"integrity": "sha512-s6Wh0BHWMfZ5VYONQwpxOYX1JeC9RKA0O9TxEzfZ6FCw2oNQb2QUPCixT9rsceKwva4+atKRw/RfU+Z7aJDmsA==",
|
"integrity": "sha512-s6Wh0BHWMfZ5VYONQwpxOYX1JeC9RKA0O9TxEzfZ6FCw2oNQb2QUPCixT9rsceKwva4+atKRw/RfU+Z7aJDmsA==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.0.6"
|
"@pushrocks/smartpromise": "^3.0.6"
|
||||||
@ -2552,7 +2556,6 @@
|
|||||||
"version": "3.1.6",
|
"version": "3.1.6",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.1.6.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.1.6.tgz",
|
||||||
"integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA==",
|
"integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/@pushrocks/smartpuppeteer": {
|
"node_modules/@pushrocks/smartpuppeteer": {
|
||||||
@ -7055,7 +7058,6 @@
|
|||||||
"version": "3.1.3",
|
"version": "3.1.3",
|
||||||
"resolved": "https://verdaccio.lossless.one/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
"resolved": "https://verdaccio.lossless.one/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||||
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/fast-glob": {
|
"node_modules/fast-glob": {
|
||||||
@ -17245,7 +17247,6 @@
|
|||||||
"version": "2.0.13",
|
"version": "2.0.13",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.13.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.13.tgz",
|
||||||
"integrity": "sha512-s6Wh0BHWMfZ5VYONQwpxOYX1JeC9RKA0O9TxEzfZ6FCw2oNQb2QUPCixT9rsceKwva4+atKRw/RfU+Z7aJDmsA==",
|
"integrity": "sha512-s6Wh0BHWMfZ5VYONQwpxOYX1JeC9RKA0O9TxEzfZ6FCw2oNQb2QUPCixT9rsceKwva4+atKRw/RfU+Z7aJDmsA==",
|
||||||
"dev": true,
|
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartpromise": "^3.0.6"
|
"@pushrocks/smartpromise": "^3.0.6"
|
||||||
}
|
}
|
||||||
@ -17548,8 +17549,7 @@
|
|||||||
"@pushrocks/smartpromise": {
|
"@pushrocks/smartpromise": {
|
||||||
"version": "3.1.6",
|
"version": "3.1.6",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.1.6.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.1.6.tgz",
|
||||||
"integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA==",
|
"integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"@pushrocks/smartpuppeteer": {
|
"@pushrocks/smartpuppeteer": {
|
||||||
"version": "1.0.36",
|
"version": "1.0.36",
|
||||||
@ -21021,8 +21021,7 @@
|
|||||||
"fast-deep-equal": {
|
"fast-deep-equal": {
|
||||||
"version": "3.1.3",
|
"version": "3.1.3",
|
||||||
"resolved": "https://verdaccio.lossless.one/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
"resolved": "https://verdaccio.lossless.one/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||||
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"fast-glob": {
|
"fast-glob": {
|
||||||
"version": "2.2.7",
|
"version": "2.2.7",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartexpect",
|
"name": "@pushrocks/smartexpect",
|
||||||
"version": "1.0.2",
|
"version": "1.0.6",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "manage expectations in code",
|
"description": "manage expectations in code",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -20,7 +20,11 @@
|
|||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
"dependencies": {
|
||||||
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
|
"fast-deep-equal": "^3.1.3"
|
||||||
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
],
|
],
|
||||||
|
23
test/test.ts
23
test/test.ts
@ -1,8 +1,25 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartexpect from '../ts/index';
|
import * as smartexpect from '../ts/index';
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('sync tests', async () => {
|
||||||
console.log(smartexpect.standardExport);
|
smartexpect.expect('hello').toBeTypeofString();
|
||||||
|
smartexpect.expect(1).not.toBeTypeofString();
|
||||||
|
smartexpect.expect(true).toBeTypeofBoolean();
|
||||||
|
smartexpect.expect(true).not.toBeTypeofNumber();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('async tests', async (toolsArg) => {
|
||||||
|
const deferred = toolsArg.defer();
|
||||||
|
toolsArg.delayFor(4000).then(() => {
|
||||||
|
deferred.resolve('hello');
|
||||||
|
});
|
||||||
|
await smartexpect.expectAsync(deferred.promise).timeout(5000).toBeTypeofString();
|
||||||
|
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.start();
|
tap.start();
|
||||||
|
12
ts/index.ts
12
ts/index.ts
@ -1,3 +1,13 @@
|
|||||||
import * as plugins from './smartexpect.plugins';
|
import * as plugins from './smartexpect.plugins';
|
||||||
|
|
||||||
export let standardExport = 'Hi there! :) This is an exported string';
|
import { Assertion } from './smartexpect.classes.assertion';
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
103
ts/smartexpect.classes.assertion.ts
Normal file
103
ts/smartexpect.classes.assertion.ts
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
import * as plugins from './smartexpect.plugins';
|
||||||
|
|
||||||
|
export type TExecutionType = 'sync' | 'async';
|
||||||
|
|
||||||
|
export class Assertion {
|
||||||
|
executionMode: TExecutionType;
|
||||||
|
baseReference: any;
|
||||||
|
private notSetting = false;
|
||||||
|
private timeoutSetting = 0;
|
||||||
|
constructor(baseReferenceArg: any, executionModeArg: TExecutionType) {
|
||||||
|
this.baseReference = baseReferenceArg;
|
||||||
|
this.executionMode = executionModeArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get not() {
|
||||||
|
this.notSetting = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public timeout(millisArg: number) {
|
||||||
|
this.timeoutSetting = millisArg;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private runCheck(checkFunction: () => any) {
|
||||||
|
const runDirectOrNegated = (checkFunction: () => any) => {
|
||||||
|
if (!this.notSetting) {
|
||||||
|
return checkFunction();
|
||||||
|
} else {
|
||||||
|
let isOk = false;
|
||||||
|
try {
|
||||||
|
runDirectOrNegated(checkFunction())
|
||||||
|
} catch (e) {
|
||||||
|
isOk = true;
|
||||||
|
}
|
||||||
|
if (!isOk) {
|
||||||
|
throw new Error('Negated assertion is not ok!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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') {
|
||||||
|
done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.baseReference.then(promiseResultArg => {
|
||||||
|
this.baseReference = promiseResultArg;
|
||||||
|
done.resolve(runDirectOrNegated(checkFunction));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return done.promise;
|
||||||
|
} else {
|
||||||
|
return runDirectOrNegated(checkFunction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public toBeTypeofString() {
|
||||||
|
return this.runCheck(() => {
|
||||||
|
if (typeof this.baseReference !== 'string') {
|
||||||
|
throw new Error(
|
||||||
|
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this.baseReference}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public toBeTypeofNumber() {
|
||||||
|
return this.runCheck(() => {
|
||||||
|
if (typeof this.baseReference !== 'number') {
|
||||||
|
throw new Error(
|
||||||
|
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this.baseReference}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public toBeTypeofBoolean() {
|
||||||
|
return this.runCheck(() => {
|
||||||
|
if (typeof this.baseReference !== 'boolean') {
|
||||||
|
throw new Error(
|
||||||
|
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this.baseReference}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public toEqual(comparisonObject: any) {
|
||||||
|
return this.runCheck(() => {
|
||||||
|
const result = plugins.fastDeepEqual(this.baseReference, comparisonObject);
|
||||||
|
if (!result) {
|
||||||
|
throw new Error(`${this.baseReference} does not equal ${comparisonObject}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,14 @@
|
|||||||
const removeme = {};
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
export { removeme };
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
export {
|
||||||
|
smartdelay,
|
||||||
|
smartpromise
|
||||||
|
}
|
||||||
|
|
||||||
|
// third party scope
|
||||||
|
import fastDeepEqual from 'fast-deep-equal';
|
||||||
|
|
||||||
|
export {
|
||||||
|
fastDeepEqual,
|
||||||
|
}
|
Reference in New Issue
Block a user