What is mock-require?
The mock-require npm package allows you to mock Node.js modules during testing. This is particularly useful for unit tests where you want to isolate the module under test from its dependencies.
What are mock-require's main functionalities?
Mocking a module
This feature allows you to replace a module with a mock implementation. In this example, the 'fs' module is mocked to return 'mocked content' when 'readFileSync' is called.
const mock = require('mock-require');
// Mock the 'fs' module
mock('fs', {
readFileSync: () => 'mocked content'
});
const fs = require('fs');
console.log(fs.readFileSync('somefile.txt')); // Outputs: 'mocked content'
// Stop mocking
mock.stop('fs');
Mocking a module with a factory function
This feature allows you to use a factory function to create the mock implementation. This can be useful if the mock needs to maintain some state or if the mock implementation is more complex.
const mock = require('mock-require');
// Mock the 'fs' module with a factory function
mock('fs', () => {
return {
readFileSync: () => 'mocked content from factory'
};
});
const fs = require('fs');
console.log(fs.readFileSync('somefile.txt')); // Outputs: 'mocked content from factory'
// Stop mocking
mock.stop('fs');
Mocking a module conditionally
This feature allows you to conditionally mock a module based on some runtime condition, such as an environment variable. This can be useful for setting up different mocks for different environments.
const mock = require('mock-require');
if (process.env.NODE_ENV === 'test') {
mock('fs', {
readFileSync: () => 'mocked content for test'
});
}
const fs = require('fs');
console.log(fs.readFileSync('somefile.txt')); // Outputs: 'mocked content for test' if NODE_ENV is 'test'
// Stop mocking
mock.stop('fs');
Other packages similar to mock-require
proxyquire
Proxyquire is a powerful tool for overriding dependencies during testing. It allows you to mock out dependencies of a module and control their behavior. Unlike mock-require, proxyquire allows you to mock dependencies on a per-require basis, which can be more flexible in certain scenarios.
rewire
Rewire is another tool for modifying module behavior during testing. It allows you to inject mocks and stubs into a module's private variables. Rewire provides more fine-grained control over the module's internals compared to mock-require, but it can be more complex to use.
sinon
Sinon is a comprehensive library for creating spies, stubs, and mocks. While it is not specifically designed for mocking require statements, it can be used in conjunction with other tools to achieve similar results. Sinon is more feature-rich and can be used for a wider range of testing scenarios compared to mock-require.
mock-require
Simple, intuitive mocking of Node.js modules.
About
mock-require is useful if you want to mock require
statements in Node.js. I wrote it because I wanted something with a straight-forward API that would let me mock anything, from a single exported function to a standard library.
Usage
var mock = require('mock-require');
mock('http', { request: function() {
console.log('http.request called');
}});
var http = require('http');
http.request();
API
mock(path, mockExport)
path: String
The module you that you want to mock. This is the same string you would pass in if you wanted to require
the module.
This path should be relative to the current file, just as it would be if you were to require
the module from the current file. mock-require is smart enough to mock this module everywhere it is required, even if it's required from a different file using a different relative path.
mockExport : object/function
The function or object you want to be returned from require
, instead of the path
module's exports.
mockExport : string
The module you want to be returned from require
, instead of the path
module's export. This allows you to replace modules with other modules. For example, if you wanted to replace the fs
module with the path
module (you probably wouldn't, but if you did):
mock('fs', 'path');
require('fs') === require('path');
This is useful if you have a mock library that you want to use in multiple places. For example:
test/spy.js
:
module.exports = function() {
return 'this was mocked';
};
test/a_spec.js
:
var mock = require('mock-require');
mock('../some/dependency', './spy');
...
test/b_spec.js
:
var mock = require('mock-require');
mock('../some/other/dependency', './spy');
...
mock.stop(path)
path: String
The module you that you want to stop mocking. This is the same string you would pass in if you wanted to require
the module.
This will only modify variables used after mock.stop
is called. For example:
var mock = require('mock-require');
mock('fs', { mockedFS: true });
var fs1 = require('fs');
mock.stop('fs');
var fs2 = require('fs');
fs1 === fs2;
mock.stopAll()
This function can be used to remove all registered mocks without the need to remove them individually using mock.stop()
.
mock('fs', {});
mock('path', {});
var fs1 = require('fs');
var path1 = require('path');
mock.stopAll();
var fs2 = require('fs');
var path2 = require('path');
fs1 === fs2;
path1 === path2;
mock.reRequire(path)
path: String
The file whose cache you want to refresh. This is useful if you're trying to mock a dependency for a file that has already been required elsewhere (possibly in another test file). Normally, Node.js will cache this file, so any mocks that you apply afterwards will have no effect. reRequire
clears the cache and allows your mock to work.
var fs = require('fs');
var fileToTest = require('./fileToTest');
mock('fs', {});
fileToTest = mock.reRequire('./fileToTest');
Note that if the file you are testing requires dependencies that in turn require the mock, those dependencies will still have the unmocked version. You may want to reRequire
all of your dependencies to ensure that your mock is always being used.
var fs = require('fs');
var otherDep = require('./otherDep')
var fileToTest = require('./fileToTest');
mock('fs', {});
otherDep = mock.reRequire('./otherDep');
fileToTest = mock.reRequire('./fileToTest');
Test
npm test