This is not a mocking library. This module allows you to require a module and pass mocks for its dependencies.
Dependencies that are not passed will be solved normally.
This module uses vm.runInNewContext and is heavily inspired by this answer in stackoverflow answer.
Install
npm install mockuire
Usage
Given a file like this one foo.js
:
var path = ; module{ return path;};
then we can test as follows:
var mockuire = module; exports { //this is the "mock" of the module path var mockedPath = { return Arrayprototypeslice; } ; //now I "mockuire" the module under test with the mocked path var foo = ; //let's see if it works: result = ; test; }
You have to pass the module in order to fallback to the module require when needed.
Private members
Two new methods will be added to the instance returned by mockuire.
Given a file like private.js
:
var count = 1;var data = foo: x: 10 { return false; } ; { return 'pong';} moduleexports { return ++count;}; moduleexports { return ;} moduleexports { return Object;}; moduleexports { return data;};
method: _private_get(name)
It allows you to get the value of a private variable:
- name (Array|string): The path of the property to get.
;
method: _private_set(name, value)
It allows you to set the value of a private variable:
- name (Array|string): The path of the property to get.
;
You can also set private members in the same moment you pass mocks for its dependencies
;
method: _private_fn(name, [mock])
It allows you to get a reference to a private function:
- name (Array|string): The path of the property to get.
;
You can also set a mock function:
{ return "Hello world!";} { return true;} var mockuire = module;var private = ;var pingMocked = private;var someFnMocked = private; ; ; ; // mocked function has a method to reset to the original function.pingMocked;someFnMocked; ; ;
Contrib - run tests
npm test
News
v2.1.0
- Support for setting and getting inner props added.
- Dependency to
resolve
module removed. - Resolution of module's path improved.
v2.0.0
- Friendly with modules of code coverage, like istanbul.
Breaking changes
Changes for 2.x
- Does not accept compilers on constructor. The version 2.x relies on the infrastrucuture of nodejs in order to load and compile a module.