soda-test
Version:
Package for Unit and API tests
283 lines • 10.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.sandbox = exports.expect = exports.useFakeTimers = exports.importPrivate = exports.rewire = exports.stub = exports.createStub = exports.spy = exports.global = exports.describe = exports.afterEach = exports.beforeEach = exports.after = exports.before = exports.pending = exports.comment = exports.it = exports.context = exports.request = exports.environment = exports.mapLibraries = exports.createAggregation = void 0;
require("reflect-metadata");
const testInfo_1 = require("./testInfo");
const executables_1 = require("./executables");
const rewire_1 = require("./rewire");
var rewire_2 = require("./rewire");
Object.defineProperty(exports, "createAggregation", { enumerable: true, get: function () { return rewire_2.createAggregation; } });
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const sinonChai = require("sinon-chai");
const rewire_3 = require("./rewire");
var rewire_4 = require("./rewire");
Object.defineProperty(exports, "mapLibraries", { enumerable: true, get: function () { return rewire_4.mapLibraries; } });
const sinons_1 = require("./sinons");
const testSuite_1 = require("./testSuite");
const testplan_1 = require("./testplan");
const environment_1 = require("./environment");
exports.environment = environment_1.environment;
let _request;
const superLib = 'supertest';
if (!rewire_1.isKarma) {
try {
_request = require(superLib);
}
catch (_a) {
_request = undefined;
}
}
exports.request = _request;
chai.use(chaiAsPromised);
chai.use(sinonChai);
(0, rewire_1.init)();
function context(text = '', extraData) {
return (target, propertyKey, descriptor) => {
if (descriptor) {
(0, testInfo_1.getInfo)(target).setMethodContext(propertyKey, text, extraData);
}
else {
(0, testInfo_1.getInfo)(target).setMemberContext(propertyKey, text, extraData);
}
};
}
exports.context = context;
function it(text, extraData) {
return (target, propertyKey, descriptor) => {
if (!text) {
text = propertyKey;
}
(0, testInfo_1.getInfo)(target).setIt(propertyKey, text, descriptor.value, extraData);
};
}
exports.it = it;
function comment(text, extraData) {
return (target, propertyKey, ignoreDescriptor) => {
(0, testInfo_1.getInfo)(target).setComment(propertyKey, text, extraData);
};
}
exports.comment = comment;
function pending() {
return (target, propertyKey, ignoreDescriptor) => {
(0, testInfo_1.getInfo)(target).setPending(propertyKey);
};
}
exports.pending = pending;
function setControlMethod(target, propertyKey, descriptor, controlName) {
const context = (0, testInfo_1.getInfo)(target).currentContext;
const contextCtrlMethods = (0, testInfo_1.getInfo)(target).getContext(context).contextControlMethods;
if (contextCtrlMethods[controlName]) {
// save the current control method, in case the override method will change context
contextCtrlMethods[controlName + '.bak'] = contextCtrlMethods[controlName];
}
contextCtrlMethods[controlName] = descriptor.value;
(0, testInfo_1.getInfo)(target).methodsContexts[propertyKey] = context;
(0, testInfo_1.getInfo)(target).lastControlMethod = { name: propertyKey, context, controlName };
}
// settomg the control method, while the controlName is given as the this argument
function ctrlMethod(target, propertyKey, descriptor) {
setControlMethod(target, propertyKey, descriptor, this);
}
const beforeMethod = ctrlMethod.bind("beforeLast");
function before() {
return beforeMethod;
}
exports.before = before;
const afterMethod = ctrlMethod.bind("afterFirst");
function after() {
return afterMethod;
}
exports.after = after;
const beforeEachMethod = ctrlMethod.bind("beforeEachLast");
function beforeEach() {
return beforeEachMethod;
}
exports.beforeEach = beforeEach;
const afterEachMethod = ctrlMethod.bind("afterEachFirst");
function afterEach() {
return afterEachMethod;
}
exports.afterEach = afterEach;
function describe(text, extraData) {
return (constructor) => {
const info = (0, testInfo_1.getInfo)(constructor.prototype);
info.describeText = text;
if (extraData)
info.extraData = extraData;
const describeBlock = new executables_1.TestDescribe(text, info, constructor);
if (exports.environment.PLAN_MODE) {
(0, testplan_1.PlanDescribe)(info);
}
else {
describeBlock.execute();
}
};
}
exports.describe = describe;
describe.mapLibraries = rewire_3.mapLibraries;
function global(value = true) {
return (target, propertyKey, parameterIndex) => {
const sinon = (0, testInfo_1.getInfo)(target).getSinon(propertyKey, parameterIndex);
if (!sinon)
return;
sinon.global = value;
};
}
exports.global = global;
function spy(spyTarget, methodName, memberName = null) {
const caller = (0, rewire_1.getCallerFileName)(1);
return (target, propertyKey, parameterIndex) => {
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, {
caller,
target: spyTarget,
method: methodName,
memberMethod: memberName,
kind: testInfo_1.SinonKind.Spy,
context: null
});
};
}
exports.spy = spy;
function createStub(stubTarget, methodName) {
const caller = (0, rewire_1.getCallerFileName)(1);
const info = {
caller,
target: stubTarget,
method: methodName,
kind: testInfo_1.SinonKind.Stub,
context: null
};
const sinonStub = (0, sinons_1.createSinon)(info);
return {
calls: (fakeMethod) => {
sinonStub.callsFake(fakeMethod);
return sinonStub;
},
returns: (value) => {
sinonStub.returns(value);
return sinonStub;
},
resolves: (value) => {
sinonStub.resolves(value);
return sinonStub;
},
rejects: (err) => {
sinonStub.throws((typeof err === 'string') ? new Error(err) : err);
return sinonStub;
},
access: (getter, setter) => {
if (getter) {
if (typeof getter === 'function') {
sinonStub.get(getter);
}
else {
sinonStub.get(() => getter);
}
}
if (setter) {
sinonStub.set(setter);
}
return sinonStub;
},
construct: (ignoreDescription) => {
throw new Error('construct stub are note supported in createStub');
}
};
}
exports.createStub = createStub;
function stub(stubTarget = null, methodName = null, memberName = null) {
const caller = (0, rewire_1.getCallerFileName)(1);
const info = {
caller,
target: stubTarget,
method: methodName,
memberMethod: memberName,
kind: testInfo_1.SinonKind.Stub,
context: null
};
const result1 = (target, propertyKey, parameterIndex) => {
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, info);
};
const result = result1;
result.calls = (fakeMethod) => {
info.setStub = { type: testInfo_1.SetStubType.Fake, value: fakeMethod };
return result;
};
result.returns = (value) => {
info.setStub = { type: testInfo_1.SetStubType.Return, value };
return result;
};
result.resolves = (value) => {
info.setStub = { type: testInfo_1.SetStubType.Resolve, value };
return result;
};
result.rejects = (err) => {
info.setStub = { type: testInfo_1.SetStubType.Reject, value: (typeof err === 'string') ? new Error(err) : err };
return result;
};
result.access = (getter, setter) => {
info.setStub = { type: testInfo_1.SetStubType.Access, value: { getter, setter } };
return result;
};
result.construct = (description) => {
info.setStub = { type: testInfo_1.SetStubType.Construct, value: description };
return result;
};
return result;
}
exports.stub = stub;
function rewire(libName, reload = false) {
const caller = (0, rewire_1.getCallerFileName)(1);
return (target, propertyKey, parameterIndex) => {
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, {
caller,
target: libName,
kind: reload ? testInfo_1.SinonKind.RewireReload : testInfo_1.SinonKind.Rewire,
context: null
});
};
}
exports.rewire = rewire;
function importPrivate(libName, methodName) {
const caller = (0, rewire_1.getCallerFileName)(1);
return (target, propertyKey, parameterIndex) => {
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, {
caller,
target: libName,
method: (methodName || propertyKey),
kind: testInfo_1.SinonKind.Import,
context: null
});
};
}
exports.importPrivate = importPrivate;
function useFakeTimers(config) {
const caller = (0, rewire_1.getCallerFileName)(1);
return (target, propertyKey, parameterIndex) => {
(0, testInfo_1.getInfo)(target).addSinon(propertyKey, parameterIndex, {
caller,
target: null,
method: null,
kind: testInfo_1.SinonKind.Timers,
timersConfig: config,
context: null
});
};
}
exports.useFakeTimers = useFakeTimers;
exports.expect = chai.expect;
if (testSuite_1.default.expect) {
exports.expect = ((actual) => {
testSuite_1.default.expect(actual)['toBe'](actual);
return chai.expect(actual);
});
exports.expect.fail = chai.expect.fail;
}
function sandbox() {
return function (prototype, propName) {
(0, testInfo_1.getInfo)(prototype).sandboxes.push(propName);
};
}
exports.sandbox = sandbox;
//# sourceMappingURL=index.js.map
;