mock-require

repository·master·Indexed 19 days ago

https://github.com/boblauer/mock-require

A utility for simple and intuitive mocking of Node.js modules. It allows developers to intercept require calls to return custom objects, functions, or other modules using mock(), stop(), stopAll(), and reRequire(). Note: This library is deprecated; proxyquire is recommended as an alternative.

Tokens
1.8K
Snippets
8
Records
9
Agent score
16%

What's inside mock-require

  1. Refresh module cache with mock.reRequire()

    master

    Use mock.reRequire(path) to clear the Node.js cache for a specific file.

    This is necessary when you want to mock a dependency for a file that has already been required elsewhere. Because Node.js caches modules, applying a mock after a file has been loaded will have no effect on that file. reRequire forces the file to be re-evaluated, allowing your new mocks to take effect.

    Important: If the file you are testing has dependencies that also require the mock, you may need to reRequire those dependencies as well to ensure consistent mocking throughout the dependency tree.

    var fs = require('fs');
    var fileToTest = require('./fileToTest');
    mock('fs', {}); // fileToTest is still using the unmocked fs module
    
    // fileToTest is now using your mock
    fileToTest = mock.reRequire('./fileToTest');
    
    // If dependencies also need the mock:
    var otherDep = require('./otherDep');
    otherDep = mock.reRequire('./otherDep');
    fileToTest = mock.reRequire('./fileToTest');
  2. Remove all mocks with mock.stopAll()

    master

    Use mock.stopAll() to clear all currently registered mocks at once. This is useful for cleaning up between tests without needing to track every individual path mocked.

    var mock = require('mock-require');
    mock('fs', {});
    mock('path', {});
    
    var fs1 = require('fs');
    var path1 = require('path');
    
    mock.stopAll();
    
    var fs2 = require('fs');
    var path2 = require('path');
    
    fs1 === fs2; // false
    path1 === path2; // false
  3. Mock Node.js modules with mock()

    master

    Use mock(path, mockExport) to intercept require calls for a specific module. This works for both built-in Node.js modules and local files.

    • path: A String representing the module to mock. Use the same string you would use in a standard require() call (e.g., 'http' or '../dependency'). mock-require handles relative paths intelligently, ensuring the mock is applied even if the module is required from different locations.
    • mockExport:
      • An object or function that will be returned instead of the original module's exports.
      • A string representing the path to another module. This allows you to replace one module with another (e.g., replacing fs with path).
    var mock = require('mock-require');
    
    // Mocking a built-in module with an object
    mock('http', { request: function() {
      console.log('http.request called');
    }});
    
    var http = require('http');
    http.request(); // 'http.request called'
    
    // Replacing one module with another
    mock('fs', 'path');
    require('fs') === require('path'); // true
  4. Stop mocking a specific module with mock.stop()

    master

    Use mock.stop(path) to remove a mock for a specific module.

    Note: This only affects variables required after mock.stop is called. Modules already required before the call will continue to use the mocked version.

    var mock = require('mock-require');
    mock('fs', { mockedFS: true });
    
    var fs1 = require('fs');
    
    mock.stop('fs');
    
    var fs2 = require('fs');
    
    fs1 === fs2; // false
  5. Reload a module with mock.reRequire()

    master

    Use mock.reRequire(path) to bypass the Node.js require.cache. This function deletes the cached version of the module at the specified path and performs a fresh require. This is useful when you want to reload a module after its underlying file or its dependencies have changed during a test run.

    const mock = require('mock-require');
    
    // Force a fresh load of a module, bypassing the cache
    const freshModule = mock.reRequire('./my-module');
  6. Clear all mocks with mock.stopAll()

    master

    Use mock.stopAll() to immediately clear all active and pending mocks. This resets the library to its original state, where all require calls return their standard Node.js module contents.

    const mock = require('mock-require');
    
    mock('./module-a', {});
    mock('./module-b', {});
    
    // Remove all mocks at once
    mock.stopAll();
  7. Intercept and replace Node.js modules with mock()

    master

    The primary function mock(path, mockExport) intercepts subsequent require calls for a specific path and replaces the returned value with mockExport.

    To use it, call mock(path, mockExport) before the module you want to intercept is required by your application code.

    Parameters:

    • path: The module path you want to mock (e.g., './my-module' or 'some-package').
    • mockExport: The value to return when the module is required. This can be:
      • An object or value representing the mock export.
      • A string representing a path to another file that should be required and returned as the mock.

    Note: This project is deprecated.

    const mock = require('mock-require');
    
    // Mocking a local module with an object
    mock('./my-module', { mocked: true });
    
    // Mocking a local module by pointing to another file
    mock('./my-module', './mocks/my-module-mock.js');
    
    // Now, when your code does:
    // const myModule = require('./my-module');
    // myModule.mocked will be true.
  8. Stop mocking a specific path with mock.stop()

    master

    Use mock.stop(path) to remove a previously established mock for a specific module. This deletes the entry from both the pending mocks and the active mocks, allowing the original module to be required again.

    const mock = require('mock-require');
    
    mock('./my-module', { mocked: true });
    
    // Later, stop mocking this specific path
    mock.stop('./my-module');