Giter VIP home page Giter VIP logo

esm-mock-loader's Introduction

ESM module mocking via Node loader

Let's mock some ESM modules. This has ZERO OVERHEAD when the loader is not in use. This was last tested on Node 16.14 but uses an experimental API that does in fact change (actually - I've had it change on me over a few months).

import * as zx from "zx";
import got from "got";

console.log(Object.keys(zx));
console.log(zx.fs.readFileSync('./package.json', 'utf8'));
console.log('Has custom method?',
'customMethod' in zx ? `Yes: ${zx.customMethod()}`: 'No');

(async () => {
    console.log(await got('https://httpbin.org/json').json());
    console.log(await got('https://httpbin.org/specialMockUrl').json()); // Will 404
})();

Run normally, no loader:

~/esm-mock via  v16.14.0
❯ node index.js
[
  '$',               'ProcessOutput',
  'ProcessPromise',  'YAML',
  'argv',            'cd',
  'chalk',           'fetch',
  'fs',              'glob',
  'globby',          'nothrow',
  'os',              'path',
  'question',        'quiet',
  'registerGlobals', 'sleep'
]
{
  "type": "module",
  "dependencies": {
    "esm": "^3.2.25",
    "got": "^12.0.1",
    "sinon": "^13.0.1",
    "zx": "^5.2.0"
  }
}

Has custom method? No
{
  slideshow: {
    author: 'Yours Truly',
    date: 'date of publication',
    slides: [ [Object], [Object] ],
    title: 'Sample Slide Show'
  }
}
node:internal/process/promises:265
            triggerUncaughtException(err, true /* fromPromise */);
HTTPError: Response code 404 (NOT FOUND)
...

Run with loader, to install mocks:

~/esm-mock via  v16.14.0
❯ node --no-warnings --experimental-loader=./loader.mjs index.js
MATCHED specifier=zx source=/hames/esm-mock/index.js mock=node_modules/zx/index.mjs
MATCHED specifier=got source=/hames/esm-mock/index.js mock=/hames/esm-mock/node_modules/got/dist/source/index.js
[
  '$',              'ProcessOutput',
  'ProcessPromise', 'YAML',
  'argv',           'cd',
  'chalk',          'customMethod',
  'fetch',          'fs',
  'glob',           'globby',
  'nothrow',        'os',
  'path',           'question',
  'quiet',          'registerGlobals',
  'sleep'
]
mockmock
Has custom method? Yes: 200
NETWORK REQ: https://httpbin.org/json
{
  slideshow: {
    author: 'Yours Truly',
    date: 'date of publication',
    slides: [ [Object], [Object] ],
    title: 'Sample Slide Show'
  }
}
MOCK NETWORK REQ: https://httpbin.org/specialMockUrl
{ "mock": "mockmockmock" }

The mocks are in the loader for now. They could be anywhere. It's all just vanilla JS. No frameworks. No magic. You'd probably want to fs.readFileSync it instead of hardcoding it in like I did.

esm-mock-loader's People

Contributors

nettypurr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.