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 have run into an issue with global functions and trying to stub or spy on them with sinon.js. It seems that whatever the Safari launcher is doing to start Safari makes it so that there are two instances of global functions.
Then use sinon to stub or spy that function, there are now two instances of myFunc.
sinon.stub(window, 'myFunc'); // spying has the same problem
console.log(window.myFunc === myFunc); //-> false
The myFunc function does not get stubbed, but the window.myFunc function does. This causes problems as calls to myFunc will still call the original function and not the stubbed one.
I have created a few test cases to demonstrate the problem. This jsFiddle shows that on all browsers including Safari, using just qunit and sinon.js and stubbing a global function behaves as expected.
I also created two test repositories using karma, one that uses Jasmine and the other that uses Mocha, to test the exact same code. In both cases, the test fail in Safari after stubbing the global function.
I've had to get around this issue by stubbing out the window function, then making the non-window function equal to the the window function. Then when I restore the function, I have to make the non-window function equal to the window function.
describe('test', function() {
beforeEach(function() {
sinon.stub(window, 'myFunc');
myFunc = window.myFunc;
});
afterEach(function() {
myFunc.restore(); // this actually doesn't restore the myFunc function, only window.myFunc
myFunc = window.myFunc;
});
});
I'm sure you could, but it's not in my current design to do so. I have a set of code that is run inside an IIFE. Only one function is exposed, but there are a plethora of internal helper functions that do a lot of work and it's important that each helper function perform correctly for the one exposed function to work as expected. I would rather test the helper functions with a variety of input rather than trying to pass input to the one exposed function as it's easier to isolate problems and deduce where a problem occurred.
I have a gulp script that concats the scripts together without the IIFE wrapper to run the tests against the functions, then renames it and concats with the IIFE wrapper as the output. The functions cannot be put on the window object as that would expose all the internal helper functions.
I have run into an issue with global functions and trying to stub or spy on them with sinon.js. It seems that whatever the Safari launcher is doing to start Safari makes it so that there are two instances of global functions.
For example, if I create a function like so:
Then use sinon to stub or spy that function, there are now two instances of
myFunc
.The
myFunc
function does not get stubbed, but thewindow.myFunc
function does. This causes problems as calls tomyFunc
will still call the original function and not the stubbed one.I have created a few test cases to demonstrate the problem. This jsFiddle shows that on all browsers including Safari, using just qunit and sinon.js and stubbing a global function behaves as expected.
I also created two test repositories using karma, one that uses Jasmine and the other that uses Mocha, to test the exact same code. In both cases, the test fail in Safari after stubbing the global function.
I've had to get around this issue by stubbing out the window function, then making the non-window function equal to the the window function. Then when I restore the function, I have to make the non-window function equal to the window function.
I posted this in karma-runner/karma-safari-launcher#11, but since it hasn't gotten any comments I'm moving it to here just for visibilities sake.
The text was updated successfully, but these errors were encountered: