Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 255
Torleif Avatar asked Nov 10 '22 03:11

Torleif


1 Answers

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!

like image 176
James Henry Avatar answered Nov 14 '22 22:11

James Henry