简体   繁体   English

如何监视默认导出函数

[英]How to spy on a default exported function

sinon.spy takes 2 parameters, object and function name. sinon.spy 有 2 个参数,对象和函数名。

I have a module as listed below:我有一个模块,如下所示:

module.exports = function xyz() { }

How do I define a spy for xyz ?如何为xyz定义间谍? I don't have object name to use.我没有要使用的对象名称。

Thoughts?想法?

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.如果您使用的是 ES6 模块导入功能,上述内容实际上不起作用,如果您是我发现您实际上可以像这样监视默认值。

// 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.htmlhttp://2ality.com/2014/09/es6-modules-final.html中所述

The default export is actually just a named export with the special name default默认导出实际上只是一个具有特殊名称 default 的命名导出

So the import * as someFunction gives you access to the whole module.exports object allowing you to spy on the default.因此 import * as someFunction 使您可以访问整个 module.exports 对象,从而使您可以监视默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM