Skip to content
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

Using sinon.js sinon.stub() or sinon.spy() does not work with global functions in Safari #1656

Closed
ghost opened this issue Apr 16, 2015 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 16, 2015

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:

function myFunc() {
  return true;
}

console.log(window.myFunc === myFunc);  //-> true

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 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.

@boneskull
Copy link
Contributor

@lambertsteven Why not just create window.myFunc instead?

window.myFunc = function myFunc() {
  return true;
}

console.log(window.myFunc === myFunc);  //-> true

@ghost
Copy link
Author

ghost commented Apr 16, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant