Requirejs-mock
About
requirejs-mock is a dependency injector for RequireJS modules. It allows you easily mock your module dependencies.
Installation
npm install requirejs-mock --save-dev
Usage example
var requirejs = ;var Injector = ; ;
API
Instantiate injector
First of all you need to create an injector that will allow you to mock your modules dependencies.
var requirejs = ; // Get Injector and provide requirejs to itvar Injector = ; ;
Maps
Setup maps
If you have a module mock in a separate file and just need to replace module with it - you should use module mapping.
;
You could also provide different maps at the same time and restore original module value:
;
Cleanup maps
If you need to get an original module instead of mapped mock - you could unmap
the module.
// unmap single moduleinjector; // unmap few modules at onceinjector; // unmap all modules mapped beforeinjector;
Notice: It's not possible to use injector.mock()
and injector.map()
for module at the same time.
Mocks
Setup mocks
If you do not have a prepared module mock in a separate file and want to do it in the runtime - you could use mocks. It's possible to provide any value (function, object, number, string, etc) as a result for the mocked module.
;
Notice: It's not possible to use injector.mock()
and injector.map()
for module at the same time.
Notice: It's not possible to mock module that has been mocked before.
Cleanup mocks
If you don't need a mock anymore it's possible to remove it. Afterwards original module value will be returned if you require the module.
// Unmock single moduleinjector; // Unmock few modules at onceinjector; // Unmock all previiously mocked modulesinjector;
Undefining a module
When you require a module - RequireJS will automatically cache it with all dependencies. If you need to provide another dependency mocks you should undefine module and then get fresh copy of module.
// Assume that you have module C that depends on module A and B// module C is required and cachedvar c = injector; // provide mocks for dependenciesinjector;injector; // undefine module C to get fresh dependencies afterwardsinjector; // get module C with mocked dependenciesc = injector;
Notice: During the mock setup - module cache will be removed automatically for you and mocked module value will be returned afterwards, as expected.
// get original A valuevar a = injector; // mock module Ainjector;injector; // => 123
Destroying the injector
You should always destroy injector to cleanup after it.
;
RequireJS versions support
requirejs-mock
supports RequireJS versions starting from 2.1.12. Previous versions are not supported.
Changelog
1.0.0 - Mar. 22, 2015
First stable release that supports all planned injector features.
- [breaking] It's not possible to
mock()
andmap()
module at the same time. Previous behavior was not specified - Added ability to remove mocks and maps used before
- Added ability to remove module from RequireJS cache
0.9.0 - Mar. 16, 2015
Initial release with basic capabilities