【发布时间】:2017-04-23 02:45:13
【问题描述】:
我正在尝试测试一个 React 组件,该组件的存储正在进行一些 gRPC 通信,因此需要 grpc node_module。我的测试是通过链式导入 grpc,因为它导入了 React 组件,该组件导入了 store,该组件导入了 grpc。
这很好,但是 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
所以我根据 Jest Documentation 在 node_modules 旁边放置一个 mocks 文件夹,并在其中创建 grpc.js:
const grpc = {};
export default grpc;
这让我更进一步,但是:
TypeError: grpc.makeGenericClientConstructor is not a function
可以理解,所以我尝试将 grpc.js 更改为:
const grpc = { makeGenericClientConstructor: () => { return; } };
但我继续收到同样的错误:
TypeError: grpc.makeGenericClientConstructor is not a function
我尝试过使用 jest.setMock 和 jest.mock,但似乎都没有帮助。
有什么想法/建议/解决方法吗?
【问题讨论】:
-
开玩笑将其评估为 ES6 模块吗?如果您使用 commonjs 而不是使用
export default grpc;,会发生什么?module.exports = grpc;? -
这已解决,谢谢!你想回答这个问题吗?
标签: javascript reactjs unit-testing jestjs grpc