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

stub a function in another file than the tested file #1910

Closed
Sufiane opened this issue Sep 25, 2018 · 7 comments
Closed

stub a function in another file than the tested file #1910

Sufiane opened this issue Sep 25, 2018 · 7 comments

Comments

@Sufiane
Copy link

Sufiane commented Sep 25, 2018

Describe the bug
I try to stub a function called in the file I'm attempting to test, this function is defined in an other file, file that I correctly require in the file I want to test (see example below).
But it seems that the stub is never called, the real function is always used no matter what.
With sinon 1.17.6 (old version I know, older project) it worked properly, the stub was used instead of the real function. Right now I'm using sinon ^6.1.5
Is there anything I forgot to do? or Did I misunderstood something?

Thanks in advance for any help !

To Reproduce
For example:
foo.js (The file I wish to test)

const bar = require('./bar')

exports.myFunction = (arg1) => {
   /* do somethings */

   const result = bar.functionToStub(arg1)

  /* keep doing things */
}

bar.js

exports.functionToStub = (arg) => {
    /* do real things */
}

foo.test.js

const sinon = require('sinon')
const bar = require('../bar')
const foo = require('../foo')

describe('test foo', () => {
   const barStub = sinon.stub(bar, 'functionToStub')
   barStub.withArgs(1).returns('a googd value!')
   barStub.throws('no stub fouuuund')

  it('should be a success', () => {
     expect(foo(1)).to.equal(/* expected result*/)
  })
  /* restore etc....*/
})

Expected behavior
I expect the function to be stubbed and the stub to be called (or to throws in this case)

Screenshots
No disponible at the moment, I could try to provide some if needed

Context (please complete the following information):

  • Library version:
    sinon: ^6.1.5
    test runner: jest
    (my older project that worked, was using mocha as test runner)
@fatso83
Copy link
Contributor

fatso83 commented Sep 26, 2018

Module loaders are not Sinon's domain. Look into link level intercepters. We have a guide

@fatso83 fatso83 closed this as completed Sep 26, 2018
@Sufiane
Copy link
Author

Sufiane commented Sep 26, 2018

If I understand correctly, now sinon only stub function from the same file, right?
It cannot stub a function coming from a dependency even though it was exported using exports.myFunction (not module.exports)

It just for the sake of understanding on how things works now :)

Thanks any how for the link and response !

@fatso83
Copy link
Contributor

fatso83 commented Sep 27, 2018

TBH I never dug into the difference in Node between module.exports and just exports, so I wouldn't know about that.

But regarding your question: no, it doesn't matter which file something is declared. What matters is how it is exposed and if it is possible to override it. That's details that you need to control, not Sinon.

For this and various ways to attack your specific issue, these issues might be worth looking into:
#1458
#1121

@Sufiane
Copy link
Author

Sufiane commented Sep 28, 2018

thanks I'll dig in how is it exposed since it appears to be the problem !

Thanks again for your help

@Sufiane
Copy link
Author

Sufiane commented Sep 28, 2018

@fatso83 Also I just found what my problem was.
In the file I wish to test is destructure my require to be able to access to the function directly:
const { theFunctionIWant } = require('path/to/module')
then I use theFunctionIWant directly

when I require it 'normally'
const moduleThatContainTheFunction = require('path/to/module')
And then use it like:
moduleThatContainTheFunction.theFunctionIWant()

the sinon stub works correctly.

Is it a normal behaviour, meaning is it normal for sinon's stub to not be called when the function is affected by destructuring ?
Or is it something we could expect it to be the normal behaviour?

@fatso83
Copy link
Contributor

fatso83 commented Sep 29, 2018

We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with sinon, so the bigger community can help answer your questions.

If you feel that your topic is an issue with Sinon, please open a new ticket and follow the guidelines for reporting an issue.


@Sufiane What you are experiencing has nothing to do with Sinon, but explaining why will further spam other maintainers. Post a question to SO. We monitor those tags, as well as do others.

@Sufiane
Copy link
Author

Sufiane commented Oct 1, 2018

@fatso83 thanks I will do !

edit: SO question and answer

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

2 participants