New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

esmock

Package Overview
Dependencies
Maintainers
1
Versions
108
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esmock

mock esm modules for unit-tests

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
increased by14.82%
Maintainers
1
Weekly downloads
 
Created
Source

esmock

(c)Bumblehead

npm version Build Status

This package must be used with node's experimental --loader,

ava --node-arguments="--loader=esmock"
mocha --loader=esmock

This package must be used with "module" type packages. Add the type to your package.json,

{
  "name": "my-module",
  "type": "module",
  "scripts" : {
    "unit-test": "ava --node-arguments=\"--loader=esmock\""
  }
}

And use it

import test from 'ava';
import esmock from 'esmock';

test('should mock module and local file at the same time', async t => {
  const main = await esmock('./local/main.js', {
    'astringifierpackage' : o => JSON.stringify(o),
    './local/util.js' : {
      exportedFunction : () => 'foobar'
    }
  });

  t.is(main(), 'foobar, ' + JSON.stringify({ test: 'object' }));
});

test('should apply third parameter "global" definitions', async t => {
  const main = await esmock('./local/main.js', {
    './local/mainUtil.js' : {
      exportedFunction : () => 'foobar'
    }
  }, {
    fs : {
      readFileSync : () => {
        return 'this value anywhere the instance imports fs, global';
      }
    }
  });

  const tplStr = main.readTemplateFile();
  t.is(tplStr, 'this value anywhere the instance imports fs, global');
});

changelog

  • 0.1.0 Apr.10.2021
    • adds support for native esm modules
  • 0.2.0 Apr.10.2021
    • adds support for mocking core modules such as fs and path
  • 0.3.0 Apr.10.2021
    • adds support for mocking modules 'globally' for the instance

Keywords

FAQs

Package last updated on 11 Apr 2021

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

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc