fix(core): update
This commit is contained in:
12
ts/index.ts
12
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;
|
||||
}
|
94
ts/smartexpect.classes.assertion.ts
Normal file
94
ts/smartexpect.classes.assertion.ts
Normal file
@ -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}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
Reference in New Issue
Block a user