How to spy on a default exported function

不羁岁月 提交于 2020-01-03 07:22:12

问题


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

I have a module as listed below:

module.exports = function xyz() { }

How do I define a spy for xyz? I don't have object name to use.

Thoughts?


回答1:


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.



来源:https://stackoverflow.com/questions/32891606/how-to-spy-on-a-default-exported-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!