babel-plugin-mockable-imports
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -8,2 +8,7 @@ # Changelog | ||
## [1.7.0] - 2020-02-01 | ||
- Support selectively restoring/undoing mocks by passing an argument to `$restore` | ||
[#23](https://github.com/robertknight/babel-plugin-mockable-imports/pull/23) | ||
## [1.6.0] - 2020-01-20 | ||
@@ -10,0 +15,0 @@ |
@@ -163,9 +163,23 @@ "use strict"; | ||
/** | ||
* Replace any active mocks with the original imports. | ||
* Replace mocks with the original imports. | ||
* | ||
* This function does nothing if called when no mocks are active. | ||
* If called with no arguments, all mocks are undone. To restore specific | ||
* mocks, pass an object whose keys are module paths and values are either | ||
* a boolean indicating whether to restore all mocks for that module, or an | ||
* object whose keys are symbols and values are booleans indicating whether | ||
* to restore mocks for the specific symbol. | ||
* | ||
* In other words, if an argument is passed, it has the same shape as the | ||
* argument to `$mock`, but the values are booleans indicating whether to | ||
* restore a mock rather than mock values. | ||
* | ||
* This function does nothing if called when no mocks are active. This enables | ||
* `$restore()` to be called unconditionally in a test cleanup function even | ||
* if the set of symbols that are mocked varies depending on the test. | ||
* | ||
* @param | ||
*/ | ||
; | ||
_proto.$restore = function $restore() { | ||
_proto.$restore = function $restore(imports) { | ||
var _this2 = this; | ||
@@ -180,4 +194,14 @@ | ||
var _this2$$meta$alias = _this2.$meta[alias], | ||
source = _this2$$meta$alias[0], | ||
symbol = _this2$$meta$alias[1], | ||
value = _this2$$meta$alias[2]; | ||
_this2[alias] = value; | ||
var restoreMock = // `$restore()` restores all mocks. | ||
typeof imports === "undefined" || // `$restore({ './module': true })` restores all mocks for './module' | ||
imports[source] === true || // `$restore({ './module': { foo: true }})` restores mock for `foo` from | ||
// './module'. | ||
typeof imports[source] === "object" && imports[source][symbol] === true; | ||
if (restoreMock) { | ||
_this2[alias] = value; | ||
} | ||
}); | ||
@@ -184,0 +208,0 @@ }; |
{ | ||
"name": "babel-plugin-mockable-imports", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Babel plugin for mocking ES imports", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -206,2 +206,27 @@ # babel-plugin-mockable-imports | ||
### Restoring specific mocks | ||
Calling `$imports.$restore()` will undo/restore all active mocks for a module. It is | ||
also possible to restore only specific mocks by passing an object which specifies | ||
the modules and symbols to un-mock. The object is in the same format as the | ||
argument to `$imports.$mock`, except the values are booleans indicating whether | ||
to restore the mock. | ||
```js | ||
// Restore all mocks for imports from the './some-widget' module. Other mocks are | ||
// left alone. | ||
$imports.$restore({ | ||
'./some-widget': true | ||
}); | ||
// Restore mocks for the "foo" symbol imported from the './utils' module. Other | ||
// mocks are left alone. | ||
$imports.$restore({ | ||
'./utils': { | ||
foo: true, | ||
} | ||
}); | ||
``` | ||
### Options | ||
@@ -208,0 +233,0 @@ |
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
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
40494
592
376