-
-
Notifications
You must be signed in to change notification settings - Fork 768
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
[Feature request] stub emits #1045
Comments
You can use any event emitter for that. Either stub |
Here are two ways how you can do it in your particular example: const stub = sinon.stub(obj, 'method', function () {
obj.emit('some event');
}); Alternatively: const stub = sinon.stub(obj, 'method');
// ...
obj.method();
obj.emit('some event'); It would be out of scope for Sinon to add library or environment specific logic for something like event emitters. If you have more questions about best practices on how to use Sinon with event emitters, please post questions on the Sinon.JS mailinglist, so the bigger community can help answer your questions. |
Got it, thanks!! |
Another approach, if you have to use import * as extend from 'extend';
import * as events from 'events';
const stub = sinon.createStubInstance(MyClass);
extend(stub, events.EventEmitter.prototype);
stub.on('event', () => { /* do something */ });
stub.method();
stub.emit('event'); |
In addition to
returns
,yields
,throws
, etc, when stubbing a method, i.e.,stub(obj, 'method')
, it would be useful to specify anemits
behavior, such as, for example:Does this seem feasible? I could try to make an implementation of this, just not sure how feasible it is realistically.
The text was updated successfully, but these errors were encountered: