Comparing version 1.1.0 to 2.0.0
@@ -1,27 +0,39 @@ | ||
var vm = require("vm"), | ||
fs = require("fs"), | ||
os = require("os"), | ||
path = require("path"); | ||
const vm = require("vm"); | ||
const fs = require("fs"); | ||
const os = require("os"); | ||
const path = require("path"); | ||
const resolve = require('resolve'); | ||
const Module = require('module'); | ||
const cache = {}; | ||
module.exports = function(caller) { | ||
module.exports = function(caller, compilers) { | ||
//get the compiled content of the file | ||
function tryGetContent(file, filePath) { | ||
var content = cache[file]; | ||
if (content) { | ||
return content; | ||
} | ||
var extCompilers = { "js": function(content) { return content; } }; | ||
// if module was already required, then we should remove | ||
// it from the require's cache in order to capture its contents | ||
var path = resolve.sync(file); | ||
delete require.cache[path]; | ||
//add the compilers parameters | ||
if(compilers){ | ||
Object.keys(compilers).forEach(function(cext){ | ||
extCompilers[cext] = compilers[cext].compile; | ||
}); | ||
} | ||
// intercepts wrap method in order to get the content of the module. | ||
const wrap = Module.wrap; | ||
Module.wrap = function() { | ||
content = arguments[0]; | ||
return wrap.apply(this, arguments); | ||
} | ||
//get the compiled content of the file | ||
function tryGetContent(file, filePath) { | ||
var ext; | ||
for (ext in extCompilers) { | ||
try { | ||
return extCompilers[ext](fs.readFileSync(file + "." + ext).toString()); | ||
} catch (error) {} | ||
} | ||
throw new Error("Cannot find module '" + filePath + "'"); | ||
// require module | ||
require(file); | ||
// restore orignal wrap function | ||
Module.wrap = wrap; | ||
// add content to local cache. | ||
cache[file] = content; | ||
return content; | ||
} | ||
@@ -28,0 +40,0 @@ |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"main": "./lib/index.js", | ||
@@ -14,0 +14,0 @@ "dependencies": {}, |
@@ -49,13 +49,2 @@ This is not a mocking library. | ||
## Optional compilers | ||
If your [SUT](http://en.wikipedia.org/wiki/System_under_test) is coffee script use this syntax: | ||
```js | ||
var mockuire = require("mockuire")(module, { "coffee": require("coffee-script") }); | ||
``` | ||
where "coffee" is the extension and the next thing needs to have a compile function. | ||
## Private members | ||
@@ -80,3 +69,3 @@ Two new methods will be added to the instance returned by mockuire. | ||
``` | ||
###method: _private_get(name)### | ||
### method: _private_get(name) | ||
It allows you to get the value of a private variable: | ||
@@ -92,3 +81,3 @@ ```js | ||
###method: _private_set(name, value)### | ||
### method: _private_set(name, value) | ||
It allows you to set the value of a private variable: | ||
@@ -118,3 +107,3 @@ ```js | ||
###method: _private_fn(name, [mock])### | ||
### method: _private_fn(name, [mock]) | ||
It allows you to get a reference to a private function: | ||
@@ -171,5 +160,16 @@ ```js | ||
## News | ||
### v2.0.0 | ||
1. Friendly with modules of code coverage, like istanbul. | ||
## Breaking changes | ||
### Changes for 2.x | ||
1. Does not accept compilers on constructor. The version 2.x relies on the infrastrucuture of nodejs in order to load and compile a module. | ||
## License | ||
[MIT License](http://www.opensource.org/licenses/mit-license.php) |
@@ -44,6 +44,5 @@ var path = require("path"); | ||
it("should allow compilers", function() { | ||
var mocker = require("../lib/index")(module, { | ||
"coffee": require("coffee-script") | ||
}), | ||
bar = mocker("./fixture/bar", { | ||
require("coffee-script"); | ||
var mocker = require("../lib/index")(module), | ||
bar = mocker("./fixture/bar.coffee", { | ||
"path": { | ||
@@ -64,4 +63,4 @@ "join": function() { | ||
(function() { | ||
return mocker("./notexist", {}); | ||
}).should["throw"]("Cannot find module './notexist'"); | ||
return mocker("./notexist"); | ||
}).should["throw"](/Cannot find module.+notexist/); | ||
}); | ||
@@ -235,1 +234,2 @@ | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
17202
10
335
5