Jasmine spy on函数导出时没有父对象,并在NODE中使用ES6导入

时间:2017-08-22 18:31:39

标签: node.js ecmascript-6 jasmine

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

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

在ES6中的OR

export function thing(event) {};

将这些文件导入测试文件时,我尝试像这样导入:

import {thing} from 'emvio-util-responses;  


//call some function that calls thing()

spyOn(???, 'thing').and.returnValue({});
expect(???.thing).toHaveBeenCalled();

我已经尝试了很多方法来完成这个但是没有调用模拟。 有些人建议导入*并提供别名作为父对象。像这样:

import * as SomeObj from 'emvio-util-responses;  


//call some function that calls thing()

spyOn(SomeObj , 'thing').and.returnValue({});
expect(SomeObj .thing).toHaveBeenCalled();

这不起作用。

其他人建议使用窗口对象

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

但是我在节点中:(。

1 个答案:

答案 0 :(得分:0)

当您使用es6模块而不是CommonJS时,所有导出都会被命名,因此您可以使用:

export default (event) => {}

然后导入&间谍为

import * as SomeObj from 'emvio-util-responses'

...
beforeEach(() => {
   spyOn(someObj, 'default')
});