mock-require
repository·master·Indexed 19 days ago
https://github.com/boblauer/mock-requireA 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.
What's inside mock-require
- This library is deprecated. For a better alternative, use proxyquire.
Refresh module cache with mock.reRequire()
masterUse
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.
reRequireforces 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
reRequirethose 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');Remove all mocks with mock.stopAll()
masterUse
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; // falseMock Node.js modules with mock()
masterUse
mock(path, mockExport)to interceptrequirecalls for a specific module. This works for both built-in Node.js modules and local files.path: AStringrepresenting the module to mock. Use the same string you would use in a standardrequire()call (e.g.,'http'or'../dependency').mock-requirehandles relative paths intelligently, ensuring the mock is applied even if the module is required from different locations.mockExport:- An
objectorfunctionthat will be returned instead of the original module's exports. - A
stringrepresenting the path to another module. This allows you to replace one module with another (e.g., replacingfswithpath).
- An
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'); // trueStop mocking a specific module with mock.stop()
masterUse
mock.stop(path)to remove a mock for a specific module.Note: This only affects variables required after
mock.stopis 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; // falseReload a module with mock.reRequire()
masterUse
mock.reRequire(path)to bypass the Node.jsrequire.cache. This function deletes the cached version of the module at the specifiedpathand performs a freshrequire. 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');Clear all mocks with mock.stopAll()
masterUse
mock.stopAll()to immediately clear all active and pending mocks. This resets the library to its original state, where allrequirecalls 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();Intercept and replace Node.js modules with mock()
masterThe primary function
mock(path, mockExport)intercepts subsequentrequirecalls for a specificpathand replaces the returned value withmockExport.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.Stop mocking a specific path with mock.stop()
masterUse
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');