You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to spy on WebSocket construction with the following code (requirebin):
sinon = require('sinon');
sinon.spy(window, 'WebSocket');
// throws an error (see console)
new window.WebSocket("ws://example.com");
In Chrome it fails with Uncaught TypeError: Failed to construct 'WebSocket': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
In Safari and PhantomJs it fails with TypeError: Attempted to wrap object property WebSocket as function
What am I doing wrong?
The text was updated successfully, but these errors were encountered:
Native objects are notoriously unreliable as spying/stubbing targets.
If you're working with a level of indirection like Socket.IO, then I'd recommend targeting that for spying/stubbing. There are examples of how to do this with Sinon.JS.
If you're working closer to the metal, then I'd recommend using a very thin wrapper around the target natives, to allow spying/stubbing.
I created wrapple for this purpose, but it's simple enough to create your own.
// totally making things up herefunctionWrapWebSocket(){returnwindow.WebSocket;}// in your codefunctioninit(){varWS=WrapWebSocket();varws=newWS();}// in your testvarspy=sinon.spy();sinon.stub(window,'WrapWebSocket',function(){returnspy;});init();assert(spy.calledWith('someurl');
Reposting this SO question:
I am trying to spy on WebSocket construction with the following code (requirebin):
In Chrome it fails with
Uncaught TypeError: Failed to construct 'WebSocket': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
In Safari and PhantomJs it fails with
TypeError: Attempted to wrap object property WebSocket as function
What am I doing wrong?
The text was updated successfully, but these errors were encountered: