如何在ecmascript 6中模拟导入的模块? [英] How can I mock an imported module in ecmascript 6?

查看:117
本文介绍了如何在ecmascript 6中模拟导入的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mocha,babel和node的测试设置,用于测试ecmascript 6代码。



有没有人有任何建议如何在被测模块中模拟进口?

解决方案

ES2015中的导入和导出是语言本身的一部分,旨在进行静态分析。因此,它们不能在运行时被操纵,这使得动态模拟不可能。



我建议您看一下实现某种形式的轻量级依赖注入框架,或动态模块解析器。



SystemJS 可能是一个很好的选择您作为通用模块加载程序。



希望有帮助!


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?

解决方案

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!

这篇关于如何在ecmascript 6中模拟导入的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆