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 = ; ; var http = ;http; // 'http.request called'
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):
; === ; // true
This is useful if you have a mock library that you want to use in multiple places. For example:
test/spy.js
:
module { return 'this was mocked';};
test/a_spec.js
:
var mock = ;;...
test/b_spec.js
:
var mock = ;;...
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 = ;; var fs1 = ; mock; var fs2 = ; fs1 === fs2; // false
mock.stopAll()
This function can be used to remove all registered mocks without the need to remove them individually using mock.stop()
.
;; var fs1 = ;var path1 = ; mock; var fs2 = ;var path2 = ; fs1 === fs2; // falsepath1 === path2; // false
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 = ;var fileToTest = ;; // fileToTest is still using the unmocked fs module fileToTest = mock; // fileToTest is now using your mock
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 = ;var otherDep = // requires fs as a dependencyvar fileToTest = ; // requires fs and otherDep as a dependency; // fileToTest and otherDep are still using the unmocked fs module otherDep = mock; // do this to make sure fs is being mocked consistentlyfileToTest = mock;
Test
npm test