简体   繁体   中英

How can I mock an imported module in ecmascript 6?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM