Socket
Socket
Sign inDemoInstall

rewire

Package Overview
Dependencies
Maintainers
2
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rewire - npm Package Compare versions

Comparing version 2.3.0 to 2.3.1

3

CHANGELOG.md
Changelog
---------
### 2.3.1
- Fix problems when global objects like JSON, etc. have been rewired [#40](https://github.com/jhnns/rewire/issues/40)
### 2.3.0

@@ -5,0 +8,0 @@ - Add possibility to mock undefined, implicit globals [#35](https://github.com/jhnns/rewire/issues/35)

28

lib/__set__.js

@@ -15,9 +15,20 @@ /**

arguments.varValue = arguments[1];
// Saving references to global objects and functions. Thus a test may even change these variables
// without interfering with rewire().
// @see https://github.com/jhnns/rewire/issues/40
arguments.refs = arguments[2] || {
isArray: Array.isArray,
TypeError: TypeError,
stringify: JSON.stringify
// We can't save eval() because eval() is a *special* global function
// That's why it can't be re-assigned in strict mode
//eval: eval
};
arguments.src = "";
arguments.revertArgs = [];
if (typeof arguments[0] === "object" && arguments.length === 1) {
if (typeof arguments[0] === "object") {
arguments.env = arguments.varName;
if (!arguments.env || Array.isArray(arguments.env)) {
throw new TypeError("__set__ expects an object as env");
if (!arguments.env || arguments.refs.isArray(arguments.env)) {
throw new arguments.refs.TypeError("__set__ expects an object as env");
}

@@ -28,3 +39,3 @@ arguments.revertArgs[0] = {};

arguments.varValue = arguments.env[arguments.varName];
arguments.src += arguments.varName + " = arguments.env[" + JSON.stringify(arguments.varName) + "]; ";
arguments.src += arguments.varName + " = arguments.env[" + arguments.refs.stringify(arguments.varName) + "]; ";
try {

@@ -39,5 +50,5 @@ // Allow tests to mock implicit globals

}
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
} else if (typeof arguments.varName === "string") {
if (!arguments.varName) {
throw new TypeError("__set__ expects a non-empty string as a variable name");
throw new arguments.refs.TypeError("__set__ expects a non-empty string as a variable name");
}

@@ -53,5 +64,8 @@ arguments.src = arguments.varName + " = arguments.varValue;";

} else {
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
throw new arguments.refs.TypeError("__set__ expects an environment object or a non-empty string as a variable name");
}
// Passing our saved references on to the revert function
arguments.revertArgs[2] = arguments.refs;
eval(arguments.src);

@@ -58,0 +72,0 @@

@@ -0,0 +0,0 @@ "use strict";

{
"name": "rewire",
"version": "2.3.0",
"version": "2.3.1",
"description": "Easy dependency injection for node.js unit testing",

@@ -5,0 +5,0 @@ "keywords": [

@@ -161,4 +161,7 @@ rewire

If `someGlobalVar` is not a valid variable name, rewire just ignores it. **In this case you're not able to override the global variable locally**.
If `someGlobalVar` is not a valid variable name, rewire just ignores it. **In this case you're not able to override the global variable locally**.
**Special globals**<br>
Please be aware that you can't rewire `eval()` or the global object itself.
<br />

@@ -165,0 +168,0 @@

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