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/spy a function #1819

Closed
jim-king-2000 opened this issue May 31, 2018 · 13 comments
Closed

stub/spy a function #1819

jim-king-2000 opened this issue May 31, 2018 · 13 comments
Assignees

Comments

@jim-king-2000
Copy link

jim-king-2000 commented May 31, 2018

I have a pure function.

  // func.mjs
  export function func() {
    return 1;
  }

How can I stub/spy it in a test file? The following code does not work. It seems sinon can only stub obj.method.

   // test.js
  import { func } from './func';
  const stub_func = sinon.stub(func).returns(2);
@mtliendo
Copy link

mtliendo commented Jun 5, 2018

import * as handyUtils from './func'

myStub = sinon.stub(handyUtils, 'func')

That should work.

@fatso83
Copy link
Contributor

fatso83 commented Jun 5, 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.

@fatso83 fatso83 closed this as completed Jun 5, 2018
@fatso83
Copy link
Contributor

fatso83 commented Jun 5, 2018

@mroderick This has been asked and answered multiple times, but I guess this ultimately points to us needing to have more how-tos/docs. This touches somewhat on the link seams, but people get confused when encountering ES6 and all the mess that entails (ES6 -> ES5 transpilation, "true" immutable ESM modules versus the ones loaded using ESM with and without the mutableNamespace option, etc).

Not asking you, btw, merely noting it :-)

@mroderick
Copy link
Member

@fatso83 you understand this issue much better than I do 😉

@jim-king-2000
Copy link
Author

Since all of my source code files are mjs files, sinon reports the following error message. It seems that we have to wait for the support of ES module.

  TypeError {
    message: 'ES Modules cannot be stubbed',
  }

@fatso83
Copy link
Contributor

fatso83 commented Jun 7, 2018

@jim-king-2000 No, shipping Sinon as a ES6 module has nothing to do with that. You need to use the mutableNamespace option I mentioned above using the esm loader in Node if you want to have mutable namespaces.

@fatso83 fatso83 self-assigned this Jun 7, 2018
@jim-king-2000
Copy link
Author

jim-king-2000 commented Jun 7, 2018

Hi @fatso83 , I need the docs about stubbing ES6 modules. No matter how I try, I always get the same error, 'ES Modules cannot be stubbed'.

@fatso83
Copy link
Contributor

fatso83 commented Jun 7, 2018

@jim-king-2000 You already got the docs ... it was the link to the NPM module. But anyway ... you might want to check out this thread: #1803 (comment)

It is the author of esm which explains to me how this option is used, and in his last comment he links to a repo with a example on how to set it up. If you look at that, you should have everything you need.

@fatso83
Copy link
Contributor

fatso83 commented Jun 7, 2018

P.S If you need any more help, @jim-king-2000, please post a question to StackOverflow and tag it with sinon, so the bigger community can help answer your questions. You can link the question back here, but note that our issue tracker is not a help forum 👮

@jim-king-2000
Copy link
Author

jim-king-2000 commented Jun 8, 2018

I read the thread and I still have no idea how to do. I use AVA + sinon with esm and I do not use babel. The following is my config. I'll post the question to StackOverflow ASAP. By the way, I insist that it is an issue, a doc issue is still an issue.

"ava": {
    "require": [
      "esm"
    ],
    "babel": false,
    "extensions": [
      "mjs"
    ]
  }

@fatso83
Copy link
Contributor

fatso83 commented Jun 8, 2018

Agreed. Added #1832. Did not find your question on SO, btw.

@jim-king-2000
Copy link
Author

I'm writing it. Thanks for the help and the advise.

@jim-king-2000
Copy link
Author

Here's the link: https://stackoverflow.com/questions/50763323/stub-a-function-from-a-es6-module-without-babel. Thank you again.

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

No branches or pull requests

4 participants