-
-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-expose lib/sinon/behaviour as a top-level export in Sinon 2 #993
Comments
I haven't thought long and hard about this, but what if we exposed a way of injecting the behavior? Instead of overwriting it. |
An injector would be nice. I suppose the most backwards-compatible way would be to pass behaviors to inject on the Something like... sinon.stub(myObject, "method", {
behaviors: {
resolves: require("sinon-as-promised").resolves,
rejects: require("sinon-as-promised").rejects
// Custom example, dispatches async Redux actions
dispatchesAsync: function(store, action) {
this.returns(action(store.dispatch, store.getState)));
}
}
}); |
That does seem a bit verbose from a client perspective ... Maybe we can come up with something friendlier. I think it is solvable, but might not be the first thing to prioritize in this transitioning phase to version 2. |
Here's a wacky idea, why don't we incorporate sinon-as-promised into Sinon.JS?
If we do decide to go that route, then the default polyfill should probably be something like https://github.com/timjansen/PinkySwear.js, as that doesn't pollute the global scope with any |
@mroderick I'm for it, since it supports a very common usecase. PinkySwear is nice, but then I contributed to it, so I am biased Not sure if it solves @HowlingEverett's issue, but even if it did, should we have a way of customizing behavior? |
Adding built-in Promise support solves it for me, so I'd be happy either way. It really comes down to 'other than Promises, is there any other gap in Sinon that would be really useful for some, but not so common they should be in core'. For example, of the top of my head, a 'rendersJSX' function on Honestly, all of these things (including Promises) are possible to work around in a hacky way using |
As long as we found some nice API to do this that won't be too bothersome or difficult to use. I honestly don't understand how the suggestion above works or is meant to illustrate, but then I haven't used |
I'm not a huge fan of that syntax either, I was just trying to think of a way to add an injector without breaking the current public API. I think I'd prefer something closer to how Example of someone using that API to extend should with sinon assertions here: https://github.com/shouldjs/sinon/blob/master/should-sinon.js Honestly, that's much closer to my original request - re-expose Behaviour. |
Just rediscovered this and wanted to note that I think we have two good ideas here
So I think we can revisit these ideas once Sinon 2.0 is out the door. Maybe as 2.1 :-) |
Whoa whoa, behavior is going away? I needed that for handling stuff like the AWS sdk, where the invocation (for some reason) returns another method, which returns a promise. |
You can now add behaviours in Sinon using the official API. See #1302 |
Without an official method for extending Sinon's stub API, a few modules—particularly sinon-as-promised—extend the available stub behaviours by patching methods onto
sinon.stub
andsinon.behavior
directly.The Sinon 2 beta version doesn't expose behavior as a top-level export anymore (rather it imports it as an internal dependency for lib/sinon/stub), which breaks these modules. While monkey-patching sinon isn't the greatest way to extend the library, it does work, and work nicely.
I attempted to solve this issue at the monkey-patch level using optional imports, but that breaks ES6/CommonJS bundlers.
Since Sinon 2 is required for Webpack/Babel module support, it'd be nice if sinon/lib/behavior was re-exposed as a top-level export to maintain compatibility with these modules.
The text was updated successfully, but these errors were encountered: