Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

esmock

Package Overview
Dependencies
Maintainers
1
Versions
107
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.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22K
increased by12.23%
Maintainers
1
Weekly downloads
 
Created
Source

esmock

npm version Build Status

esmock must be used with node's experimental --loader

{
  "name": "give-esmock-a-star",
  "type": "module",
  "scripts": {
    "test-ava": "ava --node-arguments=\"--loader=esmock\"",
    "test-mocha": "mocha --loader=esmock"
  }
}

Use it await esmock('./to/module.js', childmocks, globalmocks)

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

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

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

test('should do global instance mocks —third parameter', async t => {
  const { getFile } = await esmock('./local/main.js', {}, {
    fs : {
      readFileSync : () => {
        return 'anywhere the instance uses fs readFileSync';
      }
    }
  });

  t.is(getFile(), 'anywhere the instance uses fs readFileSync');
});

changelog

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

Keywords

FAQs

Package last updated on 14 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