How can I mock an imported module in ecmascript 6?

空扰寡人 提交于 2019-12-21 16:58:43

问题


I have a test-setup with mocha, babel and node that is meant to test ecmascript 6 code.

Does anybody have any suggestions on how to mock away imports in the module under test?


回答1:


Imports and exports in ES2015 are part of the language itself, and are designed to be statically analysable. They therefore cannot be manipulated at runtime, and that makes dynamic mocking impossible.

I would recommend that you take a look at implementing some form of lightweight dependency injection framework, or dynamic module resolver.

SystemJS could be a good choice for you as a universal module loader.

Hope that helps!



来源:https://stackoverflow.com/questions/33088439/how-can-i-mock-an-imported-module-in-ecmascript-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!