Sinon: stub/spy a function

Created on 31 May 2018  路  13Comments  路  Source: sinonjs/sinon

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);
Documentation

Most helpful comment

All 13 comments

import * as handyUtils from './func'

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

That should work.

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.

@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 :-)

@fatso83 you understand this issue much better than I do 馃槈

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',
  }

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

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

@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: https://github.com/sinonjs/sinon/pull/1803#issuecomment-391525747

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.

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 :cop:

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"
    ]
  }

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

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

Was this page helpful?
0 / 5 - 0 ratings