• Stars
    star
    442
  • Rank 98,677 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The right way to mock dependencies in Node.js or webpack environment.
                     /$$      /$$ /$$                     /$$      /$$                     /$$      
                    | $$  /$ | $$|__/                    | $$$    /$$$                    | $$      
  /$$$$$$   /$$$$$$ | $$ /$$$| $$ /$$  /$$$$$$   /$$$$$$ | $$$$  /$$$$  /$$$$$$   /$$$$$$$| $$   /$$
 /$$__  $$ /$$__  $$| $$/$$ $$ $$| $$ /$$__  $$ /$$__  $$| $$ $$/$$ $$ /$$__  $$ /$$_____/| $$  /$$/
| $$  \__/| $$$$$$$$| $$$$_  $$$$| $$| $$  \__/| $$$$$$$$| $$  $$$| $$| $$  \ $$| $$      | $$$$$$/ 
| $$      | $$_____/| $$$/ \  $$$| $$| $$      | $$_____/| $$\  $ | $$| $$  | $$| $$      | $$_  $$ 
| $$      |  $$$$$$$| $$/   \  $$| $$| $$      |  $$$$$$$| $$ \/  | $$|  $$$$$$/|  $$$$$$$| $$ \  $$
|__/       \_______/|__/     \__/|__/|__/       \_______/|__/     |__/ \______/  \_______/|__/  \__/

Build Status coverage-badge version-badge Greenkeeper badge

Quick start

1. Install

  • yarn add --dev rewiremock or npm i --save-dev rewiremock

2. Setup

I would recommend not importing rewiremock directly from tests, but create a rewiremock.js file and require it - in this way, you can preconfigure rewiremock for all tests.

for ts/es6/esm use import

// rewiremock.es6.js
import rewiremock from 'rewiremock';
// settings
// ....
rewiremock.overrideEntryPoint(module); // this is important. This command is "transfering" this module parent to rewiremock
export { rewiremock }

for commonjs/nodejs use require('rewiremock/node')

// rewiremock.cjs.js
const rewiremock = require('rewiremock/node'); 
// nothng more than `plugins.node`, but it might change how filename resolution works
/// settings
rewiremock.overrideEntryPoint(module); // this is important
module.exports = rewiremock;

for webpack

// rewiremock.es6.js
import rewiremock from 'rewiremock/webpack';
/// settings
rewiremock.overrideEntryPoint(module); // this is important
export { rewiremock }

You will also need to add a few plugins to your webpack test configuration (no need to keep them all in production).

If you import rewiremock directly from your tests, you dont need overrideEntryPoint

3. Use

There are 3 ways to mock, all with pros and cons.

proxyquire - like

Simplest one.

 const file = rewiremock.proxy('file.js', {
   'dependency':  stub
 });
 // or
 const file = rewiremock.proxy(() => require('file.js'), {
    'dependency':  stub
  });

💡to make it really "proxyquire like" - add .relative plugin. Which will allow mocking of direct dependencies only, like with proxyquire

import rewiremock, { addPlugin, plugins } from 'rewiremock';
addPlugin(plugins.relative);

mockery - like

Most powerfull one

 rewiremock('dependency').with(stub);
 rewiremock(() => require('dependency')).with(stub);
 rewiremock(() => import('dependency')).with(stub); // works with async API only
 rewiremock.enable(); 
 const file = require('file.js');
 rewiremock.disable();

jest - like

Shortest one

 // just place it next to `imports` and add a rewiremock/babel plugin 
 rewiremock('dependency').with(stub); 

💡 requires rewiremock/babel plugin

4. Tune

There are plenty of plugins to make your life easier. For example - this is my favorite setup

import { resolve } from 'path';
import rewiremock, { addPlugin, overrideEntryPoint, plugins } from 'rewiremock';
import { configure } from 'rewiremock/lib/plugins/webpack-alias'; // actually dont use it

configure(resolve(`${__dirname}/../../webpack.config.test.js`));

overrideEntryPoint(module);
// we need webpack aliases
addPlugin(plugins.webpackAlias);
// and all stub names would be a relative
addPlugin(plugins.relative);
// and all stubs should be used. Lets make it default!
addPlugin(plugins.usedByDefault);

export { rewiremock };

What command to use???!!!

// use `require` instead of just the filename to maintain type information
const mock = rewiremock.proxy(() => require('somemodule'), r => {
   'dep1': { name: 'override' },
   // use all power of rewiremock to mock something as you want...
   'dep2': r.with({name: 'override' }).toBeUsed().directChildOnly(), // use all `mocking API`
}));

There are two important things here:

  • You can use require or import to specify the file to require and the file to mock. This helps to resolve file names and maintain type information(if you have it). See Guided Mocking below.
  • You can mix simplified helpers (like .proxy) and the main API.

ESM modules

If you want to support ESM modules(powered by @std/ESM, not native modules) you have to use the import function.

  • use .module - an "async" version of .proxy
const mock = await rewiremock.module(() => import('somemodule'), {...});
  • or just use import - that would
rewiremock(() => require('xxx').with({});
rewiremock('yyy').with({});

const file = await import('somemodule');

Ok! Let's move forward!

API

main API

  • rewiremock.enable() - wipes cache and enables interceptor.
  • rewiremock.disable() - wipes cache and disables interceptor.
  • rewiremock.around(loader, creator):Promise< T > - loads a module in an asynchronous sandbox.
  • rewiremock.proxy(file, stubs):T - proxyquire like mocking api, where file is file name, and stubs are an object or a function.
  • rewiremock.module(loader, stubs):Promise< T > - async version of proxy, where loader is a function.

mocking API

  • rewiremock(moduleName: string) - fabric for a moduleNames's mock
  • rewiremock(moduleImport: loader) - async fabric for a module import function.
    • .enable/disable() - to enable or disable mock (enabled by default).
    • .with(stubs: function | Object) - overloads module with a value
    • .withDefault(stub: function | Object) - overload default es6 export
    • .es6() - marks module as ES6( __esModule )
    • .by(otherModule: string| function) - overload by another module(if string provider) or by result of a function call.
    • .callThrough() - first load the original module, and next extend it by provided stub.
    • .mockThrough([stubFactory]) - first load the original module, and then replaces all exports by stubs.
    • .dynamic() - enables hot mock updates.
    • .toBeUsed() - enables usage checking.
    • .directChildOnly - will do mock only direct dependencies.
    • .calledFromMock - will do mock only dependencies of mocked dependencies.
  • rewiremock.getMock(moduleName: string|loader) - returns existing mock (rewiremock(moduleName) will override)

isolation API

  • rewiremock.isolation() - enables isolation
  • rewiremock.withoutIsolation() - disables isolation
  • rewiremock.passBy(pattern or function) - enables some modules to pass throught isolation.

sandboxing

  • rewiremock.inScope(callback) - place synchronous callback inside a sandbox.

helper functions

  • rewiremock.stubFactory(factory) - define a stub factory for mockThrough command.

Automocking

Rewiremock supports (inspired by Jest) auto __mocks__ing.

Just create __mocks__/fileName.js, and fileName.js will be replaced by the mock. Please refer to Jest documentation for use cases.

If you don't want a particular file to be replaced by its mock - you can disable it with:

 rewiremock('fileName.js').disable();

Which API to use?

Yep - there are 4 top-level ways to activate a mock - inScope, around, proxy or just enable.

A common way to mock.

Rewiremock provides lots of APIs to help you set up mocks, and get the mocked module.

  • If everything is simple - use rewiremock.proxy. (~proxyquire)
  • If you have issues with name resolve - use rewiremock.module and resolve names by yourself.
  • If you need scope isolation - use rewiremock.around, or inScope.
  • If you need advanced syntax and type checking - use rewiremock.around.
  • You always can just use .enable/.disable (~ mockery).

All the mocks await you to provide "stubs" to override the real implementation. If you want just to ensure you have called endpoints – use rewiremock('someFile').mockThrough.

Usage

  • proxy will load a file by its own ( keep in mind - name resolution is a complex thing)
const mock = rewiremock.proxy('somemodule', (r) => ({
   'dep1': { name: 'override' },
   'dep2': r.with({name: 'override' }).toBeUsed().directChildOnly(), // use all `mocking API`
   'dep3': r.mockThrough() // automatically create a test double  
}));
  • you can require a file by yourself. ( yep, proxy is a "god" function)
const mock = rewiremock.proxy(() => require('somemodule'), {
   'dep1': { name: 'override' },
   'dep2': { onlyDump: 'stubs' }  
}));
  • or use es6 import (not for Node.js mjs real es6 modules) module is an async version of proxy, so you can use imports
const mock = await rewiremock.module(() => import('somemodule'), {
   'dep1': { name: 'override' },
   'dep2': { onlyDump: 'stubs' }  
}));
  • around - another version of .module, where you can do just anything.
const mock = await rewiremock.around(() => import('somemodule'), () => {
   rewiremock('dep1').with('something');  
   callMom();
   // prepare mocking behavior
}));
  • enable/disable - Low level API
  rewiremock('someThing').with('someThingElse')
  rewiremock.enable();
  // require something
  rewiremock.disable();

In all the cases you can specify what exactly you want to mock, or just mock anything

   addPlugin(plugins.mockThroughByDefault);  

Hoisted mocking

You can also use a top-level mocking, the same as Jest could only provide

import sinon from 'sinon';
import rewiremock from 'rewiremock';
import Component1 from 'common/Component1';
import selectors from 'common/selectors';

rewiremock('common/Component1').by('common/Component2');
rewiremock('common/Component2/action').with({ action: () => {} });
rewiremock('common/selectors').mockThrough(() => sinon.stub());

selectors.findUser.returns("cat"); // this is sinon stub.

As result Component1 will be replaced by Component2, action with empty function and all selectors by sinon stubs, with one configured.

This is only possible via babel plugin, and without it, this code will be executed without any sense, as long mocking will be configured after the files required.

Limitations

  • Other babel plugins, including JSX, won't work inside webpack hoisted code. But you may define any specific code in "functions", and let JavaScript hoist it.
  • Most variables, that you have defined in the file, are not visible to hoisted code, as long they are not yet defined. Only functions will be hoisted.
  1. Add rewiremock/babel into the plugin section of your .babelrc or babel.config.js file
// .babelrc
{
  "presets": [
    //.....
  ],
  "plugins": [
    "rewiremock/babel"
  ]
}
  1. This example will be transpiled into
import sinon from 'sinon';
import rewiremock from 'rewiremock';

rewiremock('common/Component1').by('common/Component2');
rewiremock('common/Component2/action').with({ action: () => {} });
rewiremock('common/selectors').mockThrough(() => sinon.stub());

rewiremock.enabled();

import Component1 from 'common/Component1';
import selectors from 'common/selectors';

rewiremock.disable();

selectors.findUser.returns("cat"); // this is sinon stub.

Keep in mind - rewiremock will hoist mock definition next to rewiremock import.

  • You can use anything above rewiremock import
  • You can mock anything below rewiremock import

Changing the mocks after the mocking

It is possible to partially change mocking already being applied.

 rewiremock('./foo')
  .callThrough()
  .with({ action1: action1Stub1 })
  .dynamic();

 const foo = require('./foo');
 foo.action == action1Stub1;
 
 rewiremock.getMock('./foo')
   .with({ action1: action1Stub2 });
 
 //while will RESET the mock, and could not change existing ones.
 rewiremock('./foo')
    .with({ action1: action1Stub2 });
 
 foo.action == action1Stub2;
 
 rewiremock('./foo')
    .with({ });
 
 foo.action == theRealFoo;

Changing the hoisted mocks

 import rewiremock from 'rewiremock';
 import foo from './foo';
 
 rewiremock('./foo') 
   .with({ action1: action1Stub1 })
   .dynamic();

 const fooMock = rewiremock.getMock('./foo');
 
 describe(..., () => {
   it('...', () => {
     fooMock.with({ });
     
     // while may NOT found the mock
     rewiremock.getMock('./foo').with({ });
   });
 })

Guided mocking

You may use require or import to let IDE help you to properly write fileName, and hide all filename resolution and transformation behind the scenes. But there are things you have to keep in mind

  1. Resolution of synchronous API happens on .enable
rewiremock(() => require('./fileToMock1')); // this mock would work
rewiremock.enable();
rewiremock(() => require('./fileToMock2')); //this mock WOULD NOT WORK!
  1. Using async API will throw an error
rewiremock(() => import('./fileToMock1'));  
rewiremock.enable(); // this is an exception
  1. Async API requires async API
rewiremock.module( () => import('file')) // this is ok
rewiremock.around(..., rw => rw.mock(() => import('file2'))) // this is ok

Type safety

Rewiremock can provide type-safe mocks. To enable type-safety to follow these steps:

  1. Use TypeScript or Flow.
  2. Use dynamic import syntax.
  3. Use rewiremock.around or rewiremock.module to perform a mock.
  4. Use the async form of rewiremock mock declaration.
// @flow

import rewiremock  from 'rewiremock';

rewiremock.around(
  () => import('./a.js'), 
  mock => {
  mock(() => import('./b.js'))
    .withDefault(() => "4")
    .with({testB: () => 10})
    .nonStrict() // turn off type system
    .with({ absolutely: "anything" })
  }
);

If default export is not exists on module 'b', or there is no named export testB, or types do not match - type system will throw.

If you will declare an async mock, it will not be resolved by the time of execution - Rewiremock will throw on Error.

If you have async imports inside a mocked file, follow this syntax

rewiremock.around(
  () => import('./a.js'), 
  mock => {
  // just before loader function rewiremock enabled itself
  mock(() => import('./b.js').then(mock=>mock)) // mocks `live` one `tick` more
  // just after loader function resolved rewiremock disables itself
    mock => {
    ....
    }
  }
);

Type safety for JavaScript

Rewiremock can check mock against the real implementation. This does not perform type checking, but could check exported names and exported types (function vs number, for example).

Rewiremock expects that mock will be less or equal than the original file.

rewiremock: mocked export "somethingMissing" does not exist in ./b.js
rewiremock: exported type mismatch: ./b.js:default. Expected function, got number

To activate exports comparison

 rewiremock('somemoduname')
   .toMatchOrigin(); // to activate
   
// or
import rewiremock, { addPlugin, removePlugin, plugins } from 'rewiremock';
addPlugin(plugins.alwaysMatchOrigin);   

Setup

To run with Node.js

Just use it. You can also activate node.js, which will double-check all modules names on a real FS, but... everything might work out of the box.

PS: Just use usedByDefault to ensure module names are resolved correctly.

There is also a special entry point for node.js, with nodejs plugin activated, and rewiremock as es5 export

const rewiremock = require('rewiremock/node');

// meanwhile
const rewiremock = require('rewiremock').default;

To run inside webpack environment.

Rewiremock can emulate few webpack features(like aliases) in node.js environment, but it also can be run inside webpack.

Actually rewiremock is the first client-side mocking library

But not so fast, handy. First, you have to have 3(!) Plugins enabled.

  1. webpack.NamedModulesPlugin(). To enlight the real names of modules, not "numbers". Enabled by default in webpack "dev" mode
  2. webpack.HotModuleReplacementPlugin(). To provide some information about connections between modules. Might be (and usually) already enabled, double activation of this plugin might break everything.
  3. rewiremock.webpackPlugin. To add some magic and make gears rolling.
plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new (require("rewiremock/webpack/plugin"))()
]

That's all. Now all magic will happen at the client-side.

It is better to use .proxy/module command with direct require/import and leaves all names conversion to webpack.

Hint

For better dev experience include special configuration of webpack

import rewiremock from 'rewiremock/webpack';

webpack troubleshooting

Currently, there are 2 known problems, both for mocha+webpack, ie using node.js to run webpack bundle:

  • TypeError: Cannot read property 'webpackHotUpdate' of undefined

    Caused by babel. Just don't use babel then running webpack bundles (ie babel-register). Use babel to create bundles.

  • TypeError: Cannot read property 'call' of undefined

    Caused by webpack. Sometimes it does not include some important files. To solve this problem just import('rewiremock/webpack/interceptor') in scaffolding. The problem is simply - this file does not exist in the bundle.

To actually... mock

First - define your mocks. You can do it in any place, this is just a setup.

 import rewiremock from 'rewiremock';
 ...
 
 // totaly mock `fs` with your stub 
 rewiremock('fs')
    .with({
        readFile: yourFunction
    });
  
 // replace path, by other module 
 rewiremock('path')
    .by('path-mock');
 
 // replace enzyme by preconfigured one  (from https://medium.com/airbnb-engineering/unlocking-test-performance-migrating-from-mocha-to-jest-2796c508ec50)
  rewiremock('enzyme')
     .by(({requireActual}) => {
    // see rest of possible params in d.ts file
         const enzyme = requireActual('enzyme');         
         if (!mockSetup) {
           const chai = requireActual('chai');
           const chaiEnzyme = requireActual('chai-enzyme');
           chai.use(chaiEnzyme());           
         }
         return enzyme;
     });
  
 // replace default export of ES6 module 
 rewiremock('reactComponent')
    .withDefault(MockedComponent)
     
 // replace only part of some library and keep the rest 
 rewiremock('someLibrary')
    .callThrough() 
    .with({
        onlyOneMethod
    })
    
 // secure yourself and from 'unexpected' mocks
 rewiremock('myDep')
     .with(mockedDep) 
     .calledFromMock()

Running

There is a simply way to do it: Just enable it, and dont forget to disable it later.

 //in mocha tests
 beforeEach( () => rewiremock.enable() );
 //...
 // here you will get some advantage in type casting and autocompleting.
 // it will actually works...
 const someModule = require('someModule'); 
 //...
 afterEach( () => rewiremock.disable() );

Once enabled, rewiremock will wipe all mocked modules from cache, and all modules which require them.

Including your test.

Once disabled it will restore everything.

All unrelated to test dependencies will be kept. Node modules, react, common files - everything.

As a result - it will run faster.

inScope

Sometimes you will have independent tests in a single file, and you might need separate mocks for each one. inScope execute callback inside a sandbox, and all mocks or plugins or anything else you have added will not leaks away.

 rewiremock.inScope( () => {
   rewiremock('something').with(something);
   rewiremock.enable();
   // is 'something' mocked? Yes
   ....
   rewiremock.disable();
   // is 'something' mocked? No
   // is it still listed as mock? Yes
 }); 
 // is 'something' mocked or listed? No

Around

And there is a bit harder way to do it - scope. inScope will create a new internal scope, next you can add something new to it, and then it will be destroyed. It will also enable/disable rewiremock just in time.

This helps keep tests in isolation.

PS: scopes are nesting each other as javascript prototypes do.

rewiremock.around(
    () => import('somemodule'), // load a module. Using import or require.
    // just before it you can specify mocks or anything else
    (mock) => { 
        addPlugin(nodePlugin);

        mock('./lib/a/foo').with(() => 'aa');
        mock('./lib/a/../b/bar').with(() => 'bb');
        mock('./lib/a/../b/baz').with(() => 'cc');
    }
) // at this point scope is dead
    .then((mockedBaz) => { 
        expect(mockedBaz()).to.be.equal('aabbcc');
    });

or just

rewiremock.around(() => import('somemodule')).then(mockedModule => doSomething)  

or

rewiremock.around(
    () => import('somemodule').then( mockedModule => doSomething),    
    (mock) => aPromise   
);

Currently, .inScope is the only API capable to handle es6(not node [m]js!) dynamic imports.

Proxy

Sometimes it is much easier to combine all the things.

// preferred way - create stubs using a function, where R is mock creator
rewiremock.proxy('somemodule', (r) => ({
   'dep1': { name: 'override' },
   'dep2': r.with({name: 'override' }).toBeUsed().directChildOnly() // same powerfull syntax
}));

// straight way - just provide stubs.
rewiremock.proxy('somemodule', {
   'dep1': { name: 'override' },
   'dep2': { name: 'override' }
 }));

Plugins

By default - rewiremock has limited features. You can extend its behavior via plugins.

  • relative. A bit simplistic, proxyquire-like behavior. Will override only first level dependencies, and will wipe a lot of modules from a cache. If you need override at other place - use .atAnyPlace modificator.
  • nodejs. Common support to "usual" Node.js application. Will absolutize all paths. Will wipe cache very accurately.
  • webpack-alias. deprecated. Enables you to use webpack aliases as module names. Please use node-js resolution for this.
  • childOnly. Only first level dependencies will be mocked.
  • protectNodeModules. Ensures that any module from node_modules will not be wiped from a cache.
  • toBeUsed. Adds feature. The only plugin enabled by default.
  • disabledByDefault. All mocks will be disabled on create and at the end of each cycle.
  • mockThroughByDefault. All mocks mocked through.
  • usedByDefault. All mocks to be used by the fact (reverse isolation)
import rewiremock, { addPlugin, removePlugin, plugins } from 'rewiremock';     

addPlugin(plugins.webpackAlias);
removePlugin(plugins.webpackAlias);

Nested declarations

If you import rewiremock from another place, for example, to add some defaults mocks - it will not gonna work. Each instance of rewiremock in independent. You have to pass your instance of rewiremock to build a library. PS: note, rewiremock did have nested API, but it was removed.

Isolation

Unit testing requires all dependencies to be mocked. All! To enable it, run

 rewiremock.isolation();
 //or
 rewiremock.withoutIsolation();

Then active - rewiremock will throw error on require of any unknown module.

The unknown is a module which is nor mocked, nor marked as pass-through.

To make few modules to be invisible to rewiremock, run

rewiremock.passBy(/*pattern or function*/);

rewiremock.passBy(/common/);
rewiremock.passBy(/React/);
rewiremock.passBy(/node_modules/);
rewiremock.passBy((name) => name.indexOf('.node')>=0 )

Reverse isolation

Sometimes you have to be sure, that you mock was called. Isolation will protect you then you add new dependencies, .toBeUsed protect you from removal.

Jest

Jest is a very popular testing framework, but it has one issue - is already contain mocking support.

Do not use rewiremock and jest. Even if it is possible.

Jest will not allow ANY other mocking library to coexists with Jest

To use rewiremock with Jest add to the beginning of your file

// better to disable auto mock
jest.disableAutomock();

// Jest breaks the rules, and you have to restore nesting of modules.
rewiremock.overrideEntryPoint(module);

// There is no way to use overload by Jest require or requireActual.
// use the version provided by rewiremock. 
require = rewiremock.requireActual;

!!! the last line here may disable Jest sandboxing. !!!

Also, it will disable Jest transformation, killing all the jest magics.

To be able to continue using ES6/imports - you have to enforce Babel to be applied in the common way.

describe('block of tests', () => {
  // require babel-register in describe or it block.
  // NOT! On top level. Jest sandboxing and isolation are still in action,
  // and will reset all settings to default
  require("babel-register");
})

PS: Jest will set BABEL_ENV to test.

It is better just to use rewiremock.requireActual, without overriding global require.

Your own setup.

In most cases you have to:

  • add plugin
  • setup default passBy rules
  • add some common mocks
  • do something else.

And it is not a good idea to do it in every test you have.

It is better to have one setup file, which will do everything for you

  • Part 1 - man in the middle
  // this is your test file
  
  // instead of importing original file - import your own one
  // import rewiremock from 'rewiremock';
  import rewiremock from 'test/rewiremock';    
  • Part 2 - create your own one
    // this tests/rewiremock.js
    
    import rewiremock, { addPlugin, overrideEntryPoint} from 'rewiremock';
    // do anything you need
    addPlugin(something);
    rewiremock('somemodule').with(/*....*/);   
    
    // but don't forget to add some magic
    overrideEntryPoint(module); // <-- set yourself as top module
    // PS: rewiremock will wipe this module from cache to keep magic alive.
       
    export default rewiremock;
  • Part 3 - enjoy. You extract some common code into a helper. And things become a lot easier.

Default configuration

Absolutely the same - preconfiguring rewiremock one can achieve via default configuration.

Just put rewiremock.config.js in the root dir, next to project.json, and export a configuration function

// rewiremock.config.js
import wrongrewiremock, {plugins} from 'rewiremock';

export default rewiremock => {
  // do everything with "right" rewiremock
  rewiremock.addPlugin(plugins.nodejs)
}

Caching

Default cache policy follows these steps:

  1. Preparation:
  • all files required from the original test, while interceptor is active, will bypass cache.
  • all files you indicate as mocks will be removed from the cache.
  • all "soiled" files which rely on mocks - will also be removed from the cache.
  • repeat.
  1. Finalization
  • repeat all mocks and possible "soiled" by mocks files.
  • copy over the old cache.
  • or restore the old cache completely if forceCacheClear mode is set.

The last variant is the default for proxyquire and mockery, also it is more "secure" from different side effects. Regardless, default is the first variant - as a way faster, and secure enough.

As a result of this mocking strategy, you can mock any file at any level, while keeping other files cached.

Hint

If you don't want this - just add relative plugin. It will allow mocking only for modules

required from module with parent equals entryPoint.

PS: module with parent equals entryPoint - any module you require from the test (it is an entry point). required from that module - the first level required. Simple.

Own plugins

Don't forget - you can write your own plugins. plugin is an object with fields:

{
// to transform name. Used by alias or node.js module
fileNameTransformer: (fileName, parentModule) => fileName;
// check should you wipe module or not. Never used :)
wipeCheck: (stubs, moduleName) => boolean,
// check is mocking allowed for a module. User in relative plugin
shouldMock: (mock, requestFilename, parentModule, entryPoint) => boolean
}

Extensions

Rewiremock will automatically try to resolve file

  • by specified name
  • adding .js, .jsx, .ts, .tsx, .mjs
  • you can override defaults
import {resolveExtensions} from 'rewiremock';
resolveExtensions(['.wasm', '.mjs', '.js', '.json']);

resolveExtensions is quite similar to webpack's resolve extensions.

Not working?

If something is not working - just check that you:

  • added a plugin to transform names (Node.js, webpackAlias or relative)
  • use .toBeUsed for each mocks And they were mocked. If not - rewiremock will throw an Error.

Goal

  • give the ability to mock everything - CommonJS, ES6, inside Node.js or webpack.
  • give the ability to do correctly - isolation, type checking, powerful API
  • give the ability to do it easy - simple API to cover all the cases.

Other libraries

Dependency mocking, inspired by the best libraries:

  • mockery - rewiremock is a better mockery, with the same interface.
  • proxyquire - rewiremock is a better proxyquire, with the same interface.
  • mock-require - things must not be complex, rewiremock is not.
  • jest.mocks - jest is awesome. As well as rewiremock.

Rewiremock is a better version of your favorite mocking library. It can be used with mocha, ava, karma, and anything that's not jest.

By design, rewiremock has the same behavior as Mockery. But it can behave like other libraries too, exposing handy interfaces to make mocking a joy. Supports type-safe mocking and provides TS/Flow types for itself.

Wanna read something about?

Rewiremock - medium article all by tag

Licence

MIT

Happy mocking!

More Repositories

1

react-focus-lock

It is a trap! A lock for a Focus. 🔓
JavaScript
1,092
star
2

react-imported-component

✂️📦Bundler-independent solution for SSR-friendly code-splitting
TypeScript
651
star
3

react-remove-scroll

Removes and disables 📜in a "React" way
TypeScript
611
star
4

memoize-state

The magic memoization for the State management. ✨🧠
JavaScript
324
star
5

react-focus-on

🎯 Solution for WAI ARIA compatible modal dialogs or full-screen tasks, you were looking for
TypeScript
271
star
6

use-callback-ref

🤙The same useRef, but it will callback
TypeScript
265
star
7

devolution

🦎 -> 🦖A de-evolution gun for your bundle!
JavaScript
189
star
8

react-prerendered-component

🤔Partial hydration and caching in a pre-suspense era
TypeScript
164
star
9

react-locky

"🔒-y" – Asgardian God of Event Scoping 📦, Scroll Locking 📜, Silence Casting 🙊
JavaScript
145
star
10

focus-lock

Gotcha! A11y util for scoping a focus.
TypeScript
139
star
11

vue-focus-lock

It is a trap! A lock for a Focus. A11y util for scoping a focus.
Vue
135
star
12

react-memoize

🧠 React memoization library we all deserve
JavaScript
129
star
13

react-shallow-context

☘️A speed optimization for a Context API
TypeScript
119
star
14

faste

Table based 📦 Finite State Machine 🤖
TypeScript
119
star
15

used-styles

📝All the critical styles you've used to render a page.
TypeScript
115
star
16

use-sidecar

Another way to code splitting
TypeScript
94
star
17

stylelint-semantic-groups

Opinionated rule ordering
TypeScript
92
star
18

beautiful-react-redux

Redux 🚀, Redux 🤘, Redux 🔥 - and the magic optimization
JavaScript
89
star
19

recondition

🤷‍♂️ Declarative render prop based Conditions
TypeScript
77
star
20

proxyequal

There is a bit more smart way to compare things, than a shallow equal.
JavaScript
72
star
21

kashe

A memoization library based on weakmaps. 🤯 Sometimes cache is kashe
TypeScript
63
star
22

react-remove-scroll-bar

Remove document scroll bar. Nothing more
TypeScript
59
star
23

react-scroll-locky

📜🔒 – React full-cream "anti-scroll" library, you were looking for
TypeScript
56
star
24

restate

A redux fractal state library 🕷
JavaScript
55
star
25

dom-focus-lock

It's a Trap! A11y util for scoping a focus.
JavaScript
52
star
26

react-event-injector

💉React way to addEventListener
TypeScript
45
star
27

React-stroller

🚶‍♂️Scroll as you Stroll - the most native custom scrollbars ever made
TypeScript
45
star
28

react-remock

Get any Component replaced. Anywhere. 🛠♻️
TypeScript
44
star
29

multiple-entry-points-example

a npm library with multiple entry points, all typed, all shipped in cjs/esm versions
TypeScript
43
star
30

react-reflexible

🐍 Responsible solution in a flexible form.
TypeScript
43
star
31

aria-hidden

🗣Cast aria-hidden to everything, except...
TypeScript
42
star
32

why-did-you-update-redux

Patch Redux to discover unnecessary re-renders
JavaScript
41
star
33

jsx-compress-loader

⚛️JSX optimisation loader to reduce size of React application
JavaScript
40
star
34

holistic-image

Wholesome image management
TypeScript
37
star
35

theKashey

This is me! 👨‍🔧And all the things I've done. 🧙🏻‍♂️
36
star
36

eslint-plugin-relations

Controls relationships between folders and packages 👩‍💻🤝👮🏻‍♂️
TypeScript
32
star
37

react-gearbox

⚙️📦 Gearbox - Renderless state provisioning and composition
TypeScript
30
star
38

styled-components-mixins

Use popular frameworks with Styled Components
JavaScript
30
star
39

react-on-time

Renderless composable ⏰timers and ⏱intervals
TypeScript
27
star
40

runtime-compress-loader

babel and typescript "runtime" helpers optimization loader
JavaScript
23
star
41

react-queue

⛓ Declarative task scheduler
TypeScript
23
star
42

plimited

👽Promise-based Resource Pool
TypeScript
15
star
43

flow-meter

http(s/2) chunked response meter
TypeScript
14
star
44

react-style-singleton

Simple stylesheet manager for good libraries
TypeScript
14
star
45

use-callback-state

The same `useState`, but it will callback - https://codesandbox.io/s/use-callback-state-fcxtb
TypeScript
14
star
46

require-control

Get the full control over the nodejs module system.
JavaScript
14
star
47

webpack-imported

📝stats-webpack-plugin and 💩webpack-flush-chunks had a baby!
TypeScript
13
star
48

react-rename

Create a renamed duplicate for any Component. For the Fun, and Debug ( sure 🤞)
TypeScript
11
star
49

ts-referent

🤖 project references made not by humans
TypeScript
9
star
50

partial-mock

A solution for ⬆️over and ⬇️under mocking
TypeScript
9
star
51

storybook-include

Folder based storybook configuration for monorepos
TypeScript
9
star
52

react-nyan-stroller

🐈🏳️‍🌈🏳️‍🌈🏳️‍🌈 Nyanyanyanyanyanyanya! (In form of custom scroll bar) https://codesandbox.io/s/j2l71rrxw
TypeScript
9
star
53

quadLoader

загрузчик по пирамиде
8
star
54

react-imported-library

✂Code-split any library using renderProps!
TypeScript
8
star
55

yarn-unlock-file

🧶🔓 keeping things up to date.
TypeScript
8
star
56

read-story-later

Defer storybook's story execution.
JavaScript
7
star
57

react-push-channel

🎈Context is to drill props down. React Push Channel to drill props up.
TypeScript
7
star
58

useReselect

hooks friendly reselect
TypeScript
6
star
59

address-complete

What are you looking for?
JavaScript
6
star
60

idea-exclude

Some your .ideas better be excluded
TypeScript
6
star
61

search-trie

Yet another O(n) trie. This time just and only for string search.
TypeScript
6
star
62

jest-typed-mock

Be safe!. And use the force of TS/Flow to make mocks better.
JavaScript
6
star
63

tileLoader

тайловый загрузчик для yandex2
5
star
64

react-loadable-library

Code-split any library using renderProps!
TypeScript
5
star
65

react-svg-atlas

Combine, isolate, skyrocket the SVG
JavaScript
4
star
66

the-country-names

How to explain the world? Split it into pieces and name the each one.
JavaScript
4
star
67

wipeWebpackCache

JavaScript
3
star
68

css-to-js-loader

Transforms your CSS to the `real` JS
JavaScript
3
star
69

package-self

Place yourself in the right place.
JavaScript
3
star
70

postcss-to-js

A way to create javascript code from css
JavaScript
3
star
71

react-side-channel

To the RenderProps and back.
TypeScript
3
star
72

react-dom-reflection

🔨React meets DOM-API
TypeScript
3
star
73

with-known-usage

👁ES5/ES6 compatible object key usage tracker
TypeScript
3
star
74

proxyquire-webpack-alias

Modification of proxyquire to work with webpack aliases. Proxies commonjs require/es6 import in order to allow overriding dependencies during testing.
JavaScript
3
star
75

restructor

Change your mind 🤯. Face naming standards 🤦‍♀️. Easily rename JavaScript modules 🧙🏻‍♂️.
JavaScript
3
star
76

scan-directory

Multithreaded nodejs directory scan
JavaScript
3
star
77

what-did-i-load

puppeteer based resource investigation
JavaScript
2
star
78

react-fiber-walker

Walks live React Fiber
TypeScript
2
star
79

function-double

Stand in for a function
JavaScript
2
star
80

vue-demi-focus-lock

Vue
2
star
81

derive-svg-components

Derive React components from SVG base
TypeScript
2
star
82

y2FastOverlay

Один из варианта получения скорости при работе с оверлеями напрямую
2
star
83

wipeNodeCache

JavaScript
2
star
84

compare-module-exports

JavaScript
2
star
85

flow-meter-ui

https://flow-meter-ui.vercel.app/
HTML
2
star
86

storybook-addon-poleaxe

a11y (poly) axe
TypeScript
2
star
87

4D

old school C++ stuff
C++
1
star
88

hoist-react-statics

A little helper for a better HOCs
JavaScript
1
star
89

rewiremock-simple-example

testing stuff
JavaScript
1
star
90

maps

различные околокартографические примеры
1
star
91

immutable-changes

Get will found the root change!
JavaScript
1
star
92

gulp-html-prefix

Simple gulp plugin to prefix classNames
JavaScript
1
star