esmload
This module can be use to mock the results of a native ES6 import in Node.js.
Install
$ npm i -D esmload
Usage
- Node.js process needs to be started with the following flags:
$ node --experimental-modules --loader esmload <index.mjs>
- After that, all imported modules will have an export named
'TEST_MOCK'
:
;
TEST_MOCK
is a map:
- each entry has the name of an export from the module.
- each value is a Proxy handler that can be used to manipulate the linked export.
Example
// lib1.mjs { return 'hello World!';}
// index.mjs; console; // prints 'hello World!'const mainProxyHandler = TEST_MOCK;mainProxyHandler { return Reflect + '!!';}console; // prints 'hello World!!!'mainProxyHandlerapply = undefined;console; // prints 'hello World!'
See tests for other examples.
Limitations
Right now, you can't mock something that is not an object.
Status
This is a PoC I just wrote in a couple hours. Next steps may include:
- provide a higher level interface hide the proxies to most users
- propose a dependency injection system with the same bases
- inline that in a test framework somehow