茉莉花function导出没有父对象,并在NODE中使用ES6导入

经过大量的研究,我找不到一种方法来模拟不带父对象的导出函数。 例如我试图模拟函数导出以下方式:

module.exports = function thing(event) {}; 

或在ES6

 export function thing(event) {}; 

在将这些文件导入到testing文件中时,我尝试像这样导入:

 import {thing} from 'emvio-util-responses; //call some function that calls thing() spyOn(???, 'thing').and.returnValue({}); expect(???.thing).toHaveBeenCalled(); 

我已经尝试了很多方法来完成这一点,但模拟不被称为。 有些人build议导入*并提供一个别名作为父对象。 喜欢这个:

 import * as SomeObj from 'emvio-util-responses; //call some function that calls thing() spyOn(SomeObj , 'thing').and.returnValue({}); expect(SomeObj .thing).toHaveBeenCalled(); 

这不起作用。

其他人则build议使用窗口对象

 spyOn(window, 'thing').and.returnValue({}); 

但是我在节点:(。

当你使用es6模块来代替CommonJS时,所有的导出都被命名,所以你可以使用:

 export default (event) => {} 

然后导入&spy as

 import * as SomeObj from 'emvio-util-responses' ... beforeEach(() => { spyOn(someObj, 'default') });