mirror of
https://github.com/appium/appium.git
synced 2026-04-23 20:11:08 -05:00
16910858b5
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.
41 lines
930 B
JavaScript
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 };
|