lemon-core
Version:
Lemon Serverless Micro-Service Platform
169 lines • 5.2 kB
JavaScript
/**
* `common/test-helper.ts`
* - helper functions for test
*
*
* @author Steve Jung <steve@lemoncloud.io>
* @date 2019-10-16 initial version
*
* @copyright (C) 2019 LemonCloud Co Ltd. - All Rights Reserved.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.waited = exports.marshal = exports.environ = exports._it = exports.expect2 = exports.NUL404 = exports.GETERR$ = exports.GETERR = void 0;
/**
* catch error as string
*
* ```js
* const a = sync () => throw new Error('ERROR');
* expect(await a().catch(GETERR)).toEqual('ERROR');
* ```
* @param e
*/
const GETERR = (e) => e instanceof Error ? `${e.message}` : e && typeof e == 'object' ? JSON.stringify(e) : `${e}`;
exports.GETERR = GETERR;
/**
* catch error as { error: string }
*
* ```js
* const a = sync () => throw new Error('ERROR');
* expect(await a().catch(GETERR$)).toEqual({ error:'ERROR' })
* ```
* @param e
*/
const GETERR$ = (e) => ({ error: (0, exports.GETERR)(e) });
exports.GETERR$ = GETERR$;
/**
* return null if 404 not found.
* @param e error
*/
const NUL404 = (e) => {
if (`${e.message}`.startsWith('404 NOT FOUND'))
return null;
throw e;
};
exports.NUL404 = NUL404;
/**
* improve expect() function with projection field.
*
* @param test function or data.
* @param view projection attributes.
*/
const expect2 = (test, view) => {
const project = (data) => {
if (!view)
return data;
if (data === null || data === undefined)
return data;
if (typeof data != 'object')
return data;
if (Array.isArray(data)) {
return data.map(project);
}
const views = view.split(',');
const excludes = views.filter(_ => _.startsWith('!')).map(_ => _.substring(1));
const includes = views.filter(_ => !_.startsWith('!')).map(_ => _.substring(0));
const V = excludes.reduce((N, key) => {
delete N[key];
return N;
}, data);
if (includes.length < 1)
return V; // if no includes.
return includes.reduce((N, key) => {
N[key] = V[key];
return N;
}, {});
};
try {
const ret = typeof test == 'function' ? test() : test;
if (ret instanceof Promise) {
return expect(ret.then(project).catch(exports.GETERR)).resolves;
}
else {
return expect(project(ret));
}
}
catch (e) {
return expect((0, exports.GETERR)(e));
}
};
exports.expect2 = expect2;
/**
* ignore of `it()`
*
* @param name
* @param callback
*/
const _it = (name, callback) => {
it(`ignore! ${name}`, (done) => done());
};
exports._it = _it;
/**
* use `target` as value or environment value.
* environ('PROFILE', 'none') => use env.PROFILE if exist, or 'none'
*/
const environ = (envName, envValue) => {
const $env = process.env;
const val = $env[envName] !== undefined ? $env[envName] : envValue;
return `${val || ''}`;
};
exports.environ = environ;
/**
* marshaler: convert object to dotted list.
*
* @param obj json object
* @param name current name
* @param list result list
* @param filter filter function.
*/
const marshal = (obj, filter, name = '', list = [], thiz, attr) => {
if (!filter)
throw new Error('filter is required!');
thiz = thiz === undefined ? obj : thiz;
if (obj && typeof obj == 'object') {
if (!Array.isArray(obj)) {
return Object.keys(obj).reduce((L, key) => {
const val = obj[key];
return (0, exports.marshal)(val, filter, name ? `${name}.${key}` : `${key}`, L, obj, key);
}, list);
}
else {
return obj.reduce((L, val, index) => {
return (0, exports.marshal)(val, filter, name ? `${name}.${index}` : `${index}`, L, obj, index);
}, list);
}
}
else {
const line = filter(name, obj, thiz, attr);
if (line !== undefined && line !== null)
list.push(line);
}
return list;
};
exports.marshal = marshal;
/**
* wait for some time (in msec).
*
* ```js
* await waited();
* ``
* @param t msec
*/
const waited = (t = 200) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise(resolve => {
setTimeout(() => {
resolve(undefined);
}, t);
});
});
exports.waited = waited;
//# sourceMappingURL=test-helper.js.map
;