Socket
Book a DemoInstallSign in
Socket

esmload

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esmload

Node.js ES6 loader to mock imports

0.3.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
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

mock

FAQs

Package last updated on 07 Dec 2018

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.