- Notifications
You must be signed in to change notification settings - Fork27
Simple, intuitive mocking of Node.js modules.
License
boblauer/mock-require
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This library has been deprecated. A better alternative can found athttps://www.npmjs.com/package/proxyquire.
mock-require is useful if you want to mockrequire
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.
varmock=require('mock-require');mock('http',{request:function(){console.log('http.request called');}});varhttp=require('http');http.request();// 'http.request called'
path:String
The module that you want to mock. This is the same string you would pass in if you wanted torequire
the module.
This path should be relative to the current file, just as it would be if you were torequire
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 fromrequire
, instead of thepath
module's exports.
mockExport :string
The module you want to be returned fromrequire
, instead of thepath
module's export. This allows you to replace modules with other modules. For example, if you wanted to replace thefs
module with thepath
module (you probably wouldn't, but if you did):
mock('fs','path');require('fs')===require('path');// true
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
:
varmock=require('mock-require');mock('../some/dependency','./spy');...
test/b_spec.js
:
varmock=require('mock-require');mock('../some/other/dependency','./spy');...
path:String
The module you that you want to stop mocking. This is the same string you would pass in if you wanted torequire
the module.
This will only modify variables used aftermock.stop
is called. For example:
varmock=require('mock-require');mock('fs',{mockedFS:true});varfs1=require('fs');mock.stop('fs');varfs2=require('fs');fs1===fs2;// false
This function can be used to remove all registered mocks without the need to remove them individually usingmock.stop()
.
mock('fs',{});mock('path',{});varfs1=require('fs');varpath1=require('path');mock.stopAll();varfs2=require('fs');varpath2=require('path');fs1===fs2;// falsepath1===path2;// false
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.
varfs=require('fs');varfileToTest=require('./fileToTest');mock('fs',{});// fileToTest is still using the unmocked fs modulefileToTest=mock.reRequire('./fileToTest');// 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 toreRequire
all of your dependencies to ensure that your mock is always being used.
varfs=require('fs');varotherDep=require('./otherDep')// requires fs as a dependencyvarfileToTest=require('./fileToTest');// requires fs and otherDep as a dependencymock('fs',{});// fileToTest and otherDep are still using the unmocked fs moduleotherDep=mock.reRequire('./otherDep');// do this to make sure fs is being mocked consistentlyfileToTest=mock.reRequire('./fileToTest');
npm test