2017-04-23 37 views
1

我想测试一个React组件,组件的存储正在做一些gRPC通信,因此需要grpc node_module。我的测试是通过链接导入grpc,因为它导入了React组件,该组件导入了导入grpc的存储。Jest手动模拟不工作

这是好的,但automock失败: Error: The specified module could not be found. \\?\C:\Dev\Projects\Electron\PAT\client\app\node_modules\grpc\src\node\extension_binary\grpc_node.node

所以我把一个嘲笑文件夹相邻的每玩笑Documentation node_modules和它里面我创建grpc.js:

const grpc = {}; 
export default grpc; 

这让我进一步,但: TypeError: grpc.makeGenericClientConstructor is not a function

可以理解,所以我尝试改变grpc.j s到是:

const grpc = { makeGenericClientConstructor:() => { return; } }; 

但我仍然得到同样的错误: TypeError: grpc.makeGenericClientConstructor is not a function

我使用jest.setMock和jest.mock试过,既不似乎帮助。

任何想法/建议/解决方法?

+0

是否将其评估为ES6模块?如果不是使用'export default grpc',而是使用commonjs,会发生什么? 'module.exports = grpc;'? –

+0

这个修复它谢谢你!你想回答这个问题吗? – austinrulezd00d

回答

1

不幸的是,通过使用export default grps,你的模块实际上出口的ES模块:

{ 
    default: { 
     makeGenericClientConstructor: ... 
    } 
} 

可以确认,如果你访问grpc.default.makeGenericClientConstructor这是真实的,它的存在。

如果您告诉Jest编译ES Module mocks到CommonJS(通过babel),但在您的环境中使用ES模块可以正常工作,但如果您不这样做,那么您只需要将模拟输出为CommonJS模块使用:

module.exports = grpc;