Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mockuire

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockuire - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

54

lib/index.js

@@ -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 @@

});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc