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

Stubbing method in same file doesn't work #1161

Closed
stevenmusumeche opened this issue Oct 4, 2016 · 3 comments
Closed

Stubbing method in same file doesn't work #1161

stevenmusumeche opened this issue Oct 4, 2016 · 3 comments

Comments

@stevenmusumeche
Copy link

I'm trying to unit test a function in a file while stubbing another function in the SAME file, but the mock is not being applied and the real method is being called. Here's an example:

// file: 'foo.js'

export function a() {
   // .....
}

export function b() { 
   let stuff = a(); // call a
   // ...do stuff
}

And my test:

import * as actions from 'foo';

const aStub = sinon.stub(actions, 'a').returns('mocked return');
actions.b(); // b() is executed, which calls a() instead of the expected aStub()
@fatso83
Copy link
Contributor

fatso83 commented Oct 5, 2016

Think about how this would look in standard ES5. Each function is exported with a certain name on the exports object, but it is also internally referenceable using the function name. We are talking about two different references. When stubbing you are changing the external reference. The internal reference is kept the same. You cannot change it, except if you create an explicit dependency injector (like a setter function for internal use). This is not a bug, but a feature of JavaScript.

@fatso83 fatso83 closed this as completed Oct 5, 2016
@gx761
Copy link

gx761 commented May 14, 2017

is there any workaround about this???It's quite common to get into this kind of issue when testing a nodejs file.

@fatso83
Copy link
Contributor

fatso83 commented May 14, 2017

This is covered in our documentation, in the how to section:
http://sinonjs.org/how-to/link-seams-commonjs/

tjukes pushed a commit to Giftbit/lightrail-rothschild that referenced this issue Jul 31, 2018
…stubbing

Sinon stubs don't work if the method is called from within the same file where it is defined: sinonjs/sinon#1161

This separates the code that handles one or more StripeTransactionSteps from the code that actually makes calls to Stripe.
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

3 participants