From 14ab9ee908e824ebd37be4269cf360084ae13348 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 21 Jan 2022 03:33:24 +0100 Subject: [PATCH] fix(core): update --- package-lock.json | 35 ++++------- package.json | 6 +- test/test.ts | 18 +++++- ts/index.ts | 12 +++- ts/smartexpect.classes.assertion.ts | 94 +++++++++++++++++++++++++++++ ts/smartexpect.plugins.ts | 16 ++++- 6 files changed, 150 insertions(+), 31 deletions(-) create mode 100644 ts/smartexpect.classes.assertion.ts diff --git a/package-lock.json b/package-lock.json index 1c65a53..7138671 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,11 @@ "name": "@pushrocks/smartexpect", "version": "1.0.3", "license": "MIT", + "dependencies": { + "@pushrocks/smartdelay": "^2.0.13", + "@pushrocks/smartpromise": "^3.1.6", + "chai": "^4.3.4" + }, "devDependencies": { "@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbundle": "^1.0.78", @@ -2228,7 +2233,6 @@ "version": "2.0.13", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.13.tgz", "integrity": "sha512-s6Wh0BHWMfZ5VYONQwpxOYX1JeC9RKA0O9TxEzfZ6FCw2oNQb2QUPCixT9rsceKwva4+atKRw/RfU+Z7aJDmsA==", - "dev": true, "license": "MIT", "dependencies": { "@pushrocks/smartpromise": "^3.0.6" @@ -2552,7 +2556,6 @@ "version": "3.1.6", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.1.6.tgz", "integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA==", - "dev": true, "license": "MIT" }, "node_modules/@pushrocks/smartpuppeteer": { @@ -3791,7 +3794,6 @@ "version": "1.1.0", "resolved": "https://verdaccio.lossless.one/assertion-error/-/assertion-error-1.1.0.tgz", "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", - "dev": true, "license": "MIT", "engines": { "node": "*" @@ -4650,7 +4652,6 @@ "version": "4.3.4", "resolved": "https://verdaccio.lossless.one/chai/-/chai-4.3.4.tgz", "integrity": "sha512-yS5H68VYOCtN1cjfwumDSuzn/9c+yza4f3reKXlE5rUg7SFcCEy90gJvydNgOYtblyf4Zi6jIWRnXOgErta0KA==", - "dev": true, "license": "MIT", "dependencies": { "assertion-error": "^1.1.0", @@ -4706,7 +4707,6 @@ "version": "1.0.2", "resolved": "https://verdaccio.lossless.one/check-error/-/check-error-1.0.2.tgz", "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", - "dev": true, "license": "MIT", "engines": { "node": "*" @@ -6011,7 +6011,6 @@ "version": "3.0.1", "resolved": "https://verdaccio.lossless.one/deep-eql/-/deep-eql-3.0.1.tgz", "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", - "dev": true, "license": "MIT", "dependencies": { "type-detect": "^4.0.0" @@ -7426,7 +7425,6 @@ "version": "2.0.0", "resolved": "https://verdaccio.lossless.one/get-func-name/-/get-func-name-2.0.0.tgz", "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", - "dev": true, "license": "MIT", "engines": { "node": "*" @@ -11045,7 +11043,6 @@ "version": "1.1.1", "resolved": "https://verdaccio.lossless.one/pathval/-/pathval-1.1.1.tgz", "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", - "dev": true, "license": "MIT", "engines": { "node": "*" @@ -14797,7 +14794,6 @@ "version": "4.0.8", "resolved": "https://verdaccio.lossless.one/type-detect/-/type-detect-4.0.8.tgz", "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", - "dev": true, "license": "MIT", "engines": { "node": ">=4" @@ -17245,7 +17241,6 @@ "version": "2.0.13", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.13.tgz", "integrity": "sha512-s6Wh0BHWMfZ5VYONQwpxOYX1JeC9RKA0O9TxEzfZ6FCw2oNQb2QUPCixT9rsceKwva4+atKRw/RfU+Z7aJDmsA==", - "dev": true, "requires": { "@pushrocks/smartpromise": "^3.0.6" } @@ -17548,8 +17543,7 @@ "@pushrocks/smartpromise": { "version": "3.1.6", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.1.6.tgz", - "integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA==", - "dev": true + "integrity": "sha512-v+RyVpspzYuBcV/aWXqwC03TWYeFLj7aWMG3IBDFmH2ssgug+82KLW/gw2WB2aVOEvmSkfI9OVvOQz2OsLbYlA==" }, "@pushrocks/smartpuppeteer": { "version": "1.0.36", @@ -18568,8 +18562,7 @@ "assertion-error": { "version": "1.1.0", "resolved": "https://verdaccio.lossless.one/assertion-error/-/assertion-error-1.1.0.tgz", - "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", - "dev": true + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==" }, "assign-symbols": { "version": "1.0.0", @@ -19217,7 +19210,6 @@ "version": "4.3.4", "resolved": "https://verdaccio.lossless.one/chai/-/chai-4.3.4.tgz", "integrity": "sha512-yS5H68VYOCtN1cjfwumDSuzn/9c+yza4f3reKXlE5rUg7SFcCEy90gJvydNgOYtblyf4Zi6jIWRnXOgErta0KA==", - "dev": true, "requires": { "assertion-error": "^1.1.0", "check-error": "^1.0.2", @@ -19257,8 +19249,7 @@ "check-error": { "version": "1.0.2", "resolved": "https://verdaccio.lossless.one/check-error/-/check-error-1.0.2.tgz", - "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", - "dev": true + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=" }, "chokidar": { "version": "2.1.8", @@ -20246,7 +20237,6 @@ "version": "3.0.1", "resolved": "https://verdaccio.lossless.one/deep-eql/-/deep-eql-3.0.1.tgz", "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", - "dev": true, "requires": { "type-detect": "^4.0.0" } @@ -21294,8 +21284,7 @@ "get-func-name": { "version": "2.0.0", "resolved": "https://verdaccio.lossless.one/get-func-name/-/get-func-name-2.0.0.tgz", - "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", - "dev": true + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=" }, "get-intrinsic": { "version": "1.1.1", @@ -23938,8 +23927,7 @@ "pathval": { "version": "1.1.1", "resolved": "https://verdaccio.lossless.one/pathval/-/pathval-1.1.1.tgz", - "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", - "dev": true + "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==" }, "pbkdf2": { "version": "3.1.2", @@ -26704,8 +26692,7 @@ "type-detect": { "version": "4.0.8", "resolved": "https://verdaccio.lossless.one/type-detect/-/type-detect-4.0.8.tgz", - "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", - "dev": true + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==" }, "type-fest": { "version": "2.10.0", diff --git a/package.json b/package.json index 4a81684..d6e5346 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,11 @@ "tslint": "^6.1.3", "tslint-config-prettier": "^1.15.0" }, - "dependencies": {}, + "dependencies": { + "@pushrocks/smartdelay": "^2.0.13", + "@pushrocks/smartpromise": "^3.1.6", + "chai": "^4.3.4" + }, "browserslist": [ "last 1 chrome versions" ], diff --git a/test/test.ts b/test/test.ts index e96aa4b..be9c988 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,8 +1,20 @@ -import { expect, tap } from '@pushrocks/tapbundle'; +import { tap } from '@pushrocks/tapbundle'; import * as smartexpect from '../ts/index'; -tap.test('first test', async () => { - console.log(smartexpect.standardExport); +tap.test('sync tests', async () => { + 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.start(); diff --git a/ts/index.ts b/ts/index.ts index f15613b..6483f2a 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,3 +1,13 @@ 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; +} \ No newline at end of file diff --git a/ts/smartexpect.classes.assertion.ts b/ts/smartexpect.classes.assertion.ts new file mode 100644 index 0000000..60b6a14 --- /dev/null +++ b/ts/smartexpect.classes.assertion.ts @@ -0,0 +1,94 @@ +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}` + ); + } + }); + } +} diff --git a/ts/smartexpect.plugins.ts b/ts/smartexpect.plugins.ts index 29aa9da..766c2de 100644 --- a/ts/smartexpect.plugins.ts +++ b/ts/smartexpect.plugins.ts @@ -1,2 +1,14 @@ -const removeme = {}; -export { removeme }; +import * as smartdelay from '@pushrocks/smartdelay'; +import * as smartpromise from '@pushrocks/smartpromise'; + +export { + smartdelay, + smartpromise +} + +// third party scope +import { expect } from 'chai'; + +export { + expect +} \ No newline at end of file