babel-plugin-mockable-imports
Advanced tools
Comparing version 1.7.1 to 1.8.0
19
index.js
@@ -12,2 +12,5 @@ "use strict"; | ||
const EXCLUDE_LIST = [ | ||
// Exclude imports added by this plugin. | ||
helperImportPath, | ||
// Proxyquirify and proxyquire-universal are two popular mocking libraries | ||
@@ -17,3 +20,7 @@ // which include Browserify plugins that look for references to their imports | ||
// here breaks the plugin. | ||
"proxyquire" | ||
"proxyquire", | ||
// Rollup plugins such as @rollup/plugin-babel use null-prefixed modules for | ||
// internal helpers. | ||
/\0/ | ||
]; | ||
@@ -113,3 +120,7 @@ | ||
function excludeImportsFrom(source, excludeList = EXCLUDE_LIST) { | ||
return source === helperImportPath || excludeList.includes(source); | ||
return excludeList.some( | ||
pattern => | ||
(typeof pattern === "string" && pattern === source) || | ||
(pattern instanceof RegExp && pattern.test(source)) | ||
); | ||
} | ||
@@ -399,3 +410,5 @@ | ||
const source = path.node.source.value; | ||
if (excludeImportsFrom(source, state.excludeImportsFromModules)) { | ||
if ( | ||
excludeImportsFrom(source, state.opts.excludeImportsFromModules) | ||
) { | ||
return; | ||
@@ -402,0 +415,0 @@ } |
"use strict"; | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } | ||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
@@ -17,5 +17,3 @@ function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
var MockingError = | ||
/*#__PURE__*/ | ||
function (_Error) { | ||
var MockingError = /*#__PURE__*/function (_Error) { | ||
_inheritsLoose(MockingError, _Error); | ||
@@ -28,3 +26,3 @@ | ||
return MockingError; | ||
}(_wrapNativeSuper(Error)); | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
/** | ||
@@ -40,5 +38,3 @@ * Object exposed by modules that have been processed by this plugin. | ||
var ImportMap = | ||
/*#__PURE__*/ | ||
function () { | ||
var ImportMap = /*#__PURE__*/function () { | ||
function ImportMap(imports) { | ||
@@ -201,5 +197,3 @@ if (imports === void 0) { | ||
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; | ||
imports[source] === true || typeof imports[source] === "object" && imports[source][symbol] === true; | ||
@@ -206,0 +200,0 @@ if (restoreMock) { |
{ | ||
"name": "babel-plugin-mockable-imports", | ||
"version": "1.7.1", | ||
"version": "1.8.0", | ||
"description": "Babel plugin for mocking ES imports", | ||
@@ -32,12 +32,12 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.3", | ||
"@babel/core": "^7.4.3", | ||
"@babel/plugin-syntax-jsx": "^7.2.0", | ||
"@babel/plugin-transform-destructuring": "^7.4.3", | ||
"@babel/preset-env": "^7.4.3", | ||
"@babel/cli": "^7.11.6", | ||
"@babel/core": "^7.11.6", | ||
"@babel/plugin-syntax-jsx": "^7.10.4", | ||
"@babel/plugin-transform-destructuring": "^7.10.4", | ||
"@babel/preset-env": "^7.11.5", | ||
"chai": "^4.2.0", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.1.4", | ||
"mocha": "^6.2.3", | ||
"prettier": "1.16.4" | ||
} | ||
} |
@@ -243,5 +243,8 @@ # babel-plugin-mockable-imports | ||
An array of module names which should be ignored when processing imports. | ||
Any imports from these modules will not be mockable. | ||
Default: `["proxyquire"]`. | ||
Any imports from these modules will not be mockable. Module names can be | ||
specified as strings (to match exactly) or regular expressions. | ||
By default this list includes imports from a few packages (eg. proxyquire, | ||
@rollup/plugin-babel) which are known not to work well with this plugin. | ||
### CommonJS support | ||
@@ -299,3 +302,3 @@ | ||
## Known issues and limitations | ||
## Common problems and errors | ||
@@ -321,2 +324,22 @@ ### Mocking code that runs when a module is imported | ||
### "Module not does import..." error when calling `$imports.$mock` | ||
You may get this error when calling `$imports.$mock` if the module name or symbol | ||
does not match one that has been registered as an import of the module. | ||
Common reasons this can happen are: | ||
- A misspelling in the file path or symbol name passed to `$mock`. Check that the | ||
path and symbol name match what is used in the module being tested. | ||
- Your code is transformed before being processed by this plugin in a way that | ||
modifies the imports. This may happen, for example, if you are compiling code | ||
from another language into JavaScript before applying this plugin. | ||
The available symbols to mock can be found by inspecting `$imports.$meta`. | ||
This is an object that maps the name of the symbol in the file to the location | ||
that it comes from. | ||
_Note: $meta is not considered a public API of this plugin and its shape may | ||
change in minor or patch releases_. | ||
### `$imports` export conflicts | ||
@@ -327,3 +350,3 @@ | ||
We may in future add an alternative method of exposing the `$imports` object so that tests can get at it. | ||
### Dynamic imports | ||
@@ -330,0 +353,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
605
399
39829
4