How to spy on a default exported function

后端 未结 1 1982
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 02:52

sinon.spy takes 2 parameters, object and function name.

I have a module as listed below:

module.exports = function xyz() { }

How do I define

相关标签:
1条回答
  • 2021-02-19 03:07

    The above actually doesn't work if you're using the ES6 modules import functionality, If you are I've discovered you can actually spy on the defaults like so.

    // your file
    export default function () {console.log('something here');}
    
    // your test
    import * as someFunction from './someFunction';
    spyOn(someFunction, 'default')
    

    As stated in http://2ality.com/2014/09/es6-modules-final.html

    The default export is actually just a named export with the special name default

    So the import * as someFunction gives you access to the whole module.exports object allowing you to spy on the default.

    0 讨论(0)
提交回复
热议问题