Skip to content

Commit

Permalink
Fixes sinonjs#1389: add .usePromise for global promise library usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Druotic committed Aug 22, 2017
1 parent 17bf9e6 commit abb5852
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 4 deletions.
9 changes: 7 additions & 2 deletions docs/release-source/release/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Creates a new object with the given function as the protoype and stubs all imple
class Container {
contains(item) { /* ... */ }
}

var stubContainer = sinon.createStubInstance(Container);
stubContainer.contains.returns(false);
stubContainer.contains.withArgs("item").returns(true);
```

The given constructor function is not invoked. See also the [stub API](../stubs).
The given constructor function is not invoked. See also the [stub API](./stubs).

#### `sinon.format(object);`

Expand All @@ -60,3 +60,8 @@ sinon.log = function (message) {
jstestdriver.console.log(message);
};
```

#### `sinon.usePromise(promiseLibrary);`

Ensures all future stubs will use the provided promise library instead of native
promises. For per-stub promise overriding, see the [stub API.](./stubs)
7 changes: 6 additions & 1 deletion lib/sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ exports.sandbox = sandbox;
exports.expectation = require("./sinon/mock-expectation");
exports.createStubInstance = require("./sinon/stub").createStubInstance;

exports.defaultConfig = require("./sinon/util/core/default-config");
var defaultConfig = require("./sinon/util/core/default-config");
exports.defaultConfig = defaultConfig;

var fakeTimers = require("./sinon/util/fake_timers");
exports.useFakeTimers = fakeTimers.useFakeTimers;
Expand All @@ -38,5 +39,9 @@ exports.addBehavior = function (name, fn) {
behavior.addBehavior(exports.stub, name, fn);
};

exports.usePromise = function (promiseLibrary) {
defaultConfig.promiseLibrary = promiseLibrary;
};

var format = require("./sinon/util/core/format");
exports.setFormatter = format.setFormatter;
12 changes: 11 additions & 1 deletion lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var extend = require("./util/core/extend");
var functionName = require("./util/core/function-name");
var valueToString = require("./util/core/value-to-string");
var getConfig = require("./util/core/get-config");

var slice = Array.prototype.slice;
var join = Array.prototype.join;
Expand Down Expand Up @@ -104,8 +105,12 @@ var proto = {
delete behavior.addBehavior;
delete behavior.createBehavior;
behavior.stub = stub;
behavior.promiseLibrary = getConfig().promiseLibrary;

if (!behavior.promiseLibrary &&
stub.defaultBehavior &&
stub.defaultBehavior.promiseLibrary) {

if (stub.defaultBehavior && stub.defaultBehavior.promiseLibrary) {
behavior.promiseLibrary = stub.defaultBehavior.promiseLibrary;
}

Expand Down Expand Up @@ -218,6 +223,11 @@ function addBehavior(stub, name, fn) {
stub[name] = createBehavior(name);
}

function usePromise(promiseLibrary) {
proto.promiseLibrary = promiseLibrary;
}

proto.addBehavior = addBehavior;
proto.createBehavior = createBehavior;
proto.usePromise = usePromise;
module.exports = proto;
57 changes: 57 additions & 0 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var sinonSandbox = require("../lib/sinon/sandbox");
var sinonCollection = require("../lib/sinon/collection");
var sinonSpy = require("../lib/sinon/spy");
var sinonStub = require("../lib/sinon/stub");
var usePromise = require("../lib/sinon").usePromise;
var defaultConfig = require("../lib/sinon/util/core/default-config");
var sinonConfig = require("../lib/sinon/util/core/get-config");
var sinonMatch = require("../lib/sinon/match");
var sinonAssert = require("../lib/sinon/assert");
Expand Down Expand Up @@ -126,6 +128,61 @@ describe("sinonSandbox", function () {
});
});

// sinon.usePromise
describe(".usePromise (global)", function () {
beforeEach(function () {
this.sandbox = Object.create(sinonSandbox);
});

afterEach(function () {
delete defaultConfig.promiseLibrary;
});

it("must set all stubs created from sandbox with mockPromise", function () {
if (!supportPromise) { return this.skip(); }

var resolveValue = {};
var mockPromise = {
resolve: sinonStub.create().resolves(resolveValue)
};

usePromise(mockPromise);
var stub = this.sandbox.stub().resolves();

return stub()
.then(function (action) {

assert.same(resolveValue, action);
assert(mockPromise.resolve.calledOnce);
});
});

it("must set all stubs created from sandbox with mockPromise (object)", function () {
if (!supportPromise) { return this.skip(); }

var resolveValue = {};
var mockPromise = {
resolve: sinonStub.create().resolves(resolveValue)
};
var stubbedObject = {
stubbedMethod: function () {
return;
}
};

usePromise(mockPromise);
this.sandbox.stub(stubbedObject);
stubbedObject.stubbedMethod.resolves({});

return stubbedObject.stubbedMethod()
.then(function (action) {

assert.same(resolveValue, action);
assert(mockPromise.resolve.calledOnce);
});
});
});

describe(".usingPromise", function () {
beforeEach(function () {
this.sandbox = Object.create(sinonSandbox);
Expand Down
57 changes: 57 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var referee = require("referee");
var createStub = require("../lib/sinon/stub");
var createStubInstance = require("../lib/sinon/stub").createStubInstance;
var createSpy = require("../lib/sinon/spy");
var usePromise = require("../lib/sinon").usePromise;
var defaultConfig = require("../lib/sinon/util/core/default-config");
var sinonMatch = require("../lib/sinon/match");
var getPropertyDescriptor = require("../lib/sinon/util/core/get-property-descriptor");
var deprecated = require("../lib/sinon/util/core/deprecated");
Expand Down Expand Up @@ -537,6 +539,61 @@ describe("stub", function () {
});
});

// sinon.usePromise
describe(".usePromise (global)", function () {

afterEach(function () {
delete defaultConfig.promiseLibrary;
});

it("should exist and be a function", function () {
assert(usePromise);
assert.isFunction(usePromise);
});

it("should set the promise used by resolve", function () {
var promise = {
resolve: createStub.create().callsFake(function (value) {
return Promise.resolve(value);
})
};
usePromise(promise);

var stub = createStub.create();
var object = {};

stub.resolves(object);

return stub().then(function (actual) {
assert.same(actual, object, "Same object resolved");
assert.isTrue(promise.resolve.calledOnce, "Custom promise resolve called once");
assert.isTrue(promise.resolve.calledWith(object), "Custom promise resolve called once with expected");
});
});

it("should set the promise used by reject", function () {
var promise = {
reject: createStub.create().callsFake(function (err) {
return Promise.reject(err);
})
};
usePromise(promise);

var stub = createStub.create();
var reason = new Error();

stub.rejects(reason);

return stub().then(function () {
referee.fail("this should not resolve");
}).catch(function (actual) {
assert.same(actual, reason, "Same object resolved");
assert.isTrue(promise.reject.calledOnce, "Custom promise reject called once");
assert.isTrue(promise.reject.calledWith(reason), "Custom promise reject called once with expected");
});
});
});

describe(".throws", function () {
it("throws specified exception", function () {
var stub = createStub.create();
Expand Down

0 comments on commit abb5852

Please sign in to comment.