Files
appium/packages/test-support/lib/sandbox-utils.js
T
Christopher Hiller 16910858b5 chore: add @appium/test-support to monorepo & use Bluebird w/ Sinon
sinon supports promises natively, and we are using bluebird here.  promise resolution seems to go wonky when using sinon's stub `.returns(B.reject(err))`, so now we tell sinon to use _bluebird's_ promises, which makes `.rejects(err)` do the same thing except better.
2021-05-18 12:49:28 -07:00

41 lines
930 B
JavaScript

import sinon from 'sinon';
import _ from 'lodash';
import B from 'bluebird';
let SANDBOX = Symbol();
// use this one if using a mix of mocks/stub/spies
function withSandbox (config, fn) {
return () => {
const S = {
mocks: {},
verify () {
return this.sandbox.verify();
},
};
beforeEach(function beforeEach () {
S.sandbox = sinon.createSandbox();
S.sandbox.usingPromise(B);
S.mocks[SANDBOX] = S.sandbox;
for (let [key, value] of _.toPairs(config.mocks)) {
S.mocks[key] = S.sandbox.mock(value);
}
});
afterEach(function afterEach () {
S.sandbox.restore();
for (let k of _.keys(S.mocks)) {
delete S.mocks[k];
}
delete S.mocks[SANDBOX];
});
fn(S);
};
}
function verifySandbox (obj) {
let sandbox = obj.sandbox ? obj.sandbox : obj[SANDBOX];
sandbox.verify();
}
export { withSandbox, verifySandbox };