Socket
Socket
Sign inDemoInstall

esmload

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    esmload

Node.js ES6 loader to mock imports


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
4.82 kB
Created
Weekly downloads
 

Readme

Source

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':
import { main, TEST_MOCK } from './lib1.mjs';

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
export function main() {
    return 'hello World!';
}
// index.mjs
import { main, TEST_MOCK } from './lib1.mjs';

console.log(main()); // prints 'hello World!'
const mainProxyHandler = TEST_MOCK.get('main');
mainProxyHandler.apply = function(target, self, args) {
    return Reflect.apply(target, self, args) +  '!!';
}
console.log(main()); // prints 'hello World!!!'
mainProxyHandler.apply = undefined;
console.log(main()); // 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

Keywords

FAQs

Last updated on 07 Dec 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc