babel-plugin-mockable-imports
Advanced tools
Comparing version 1.8.0 to 2.0.0
@@ -1,8 +0,5 @@ | ||
"use strict"; | ||
import * as pathModule from "path"; | ||
const pathModule = require("path"); | ||
const helperImportPath = "babel-plugin-mockable-imports/lib/helpers"; | ||
const packageName = require("./package.json").name; | ||
const helperImportPath = `${packageName}/lib/helpers`; | ||
/** | ||
@@ -35,3 +32,3 @@ * Default list of modules whose imports are excluded from processing. | ||
module.exports = ({ types: t }) => { | ||
export default ({ types: t }) => { | ||
/** | ||
@@ -38,0 +35,0 @@ * Return the required module from a CallExpression, if it is a `require(...)` |
@@ -1,26 +0,7 @@ | ||
"use strict"; | ||
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 _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; } } | ||
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
var MockingError = /*#__PURE__*/function (_Error) { | ||
_inheritsLoose(MockingError, _Error); | ||
function MockingError(msg) { | ||
return _Error.call(this, msg) || this; | ||
export class MockingError extends Error { | ||
constructor(msg) { | ||
super(msg); | ||
} | ||
return MockingError; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
} | ||
/** | ||
@@ -35,5 +16,4 @@ * Object exposed by modules that have been processed by this plugin. | ||
var ImportMap = /*#__PURE__*/function () { | ||
function ImportMap(imports) { | ||
export class ImportMap { | ||
constructor(imports) { | ||
if (imports === void 0) { | ||
@@ -58,5 +38,3 @@ imports = {}; | ||
var _proto = ImportMap.prototype; | ||
_proto.$add = function $add(alias, source, symbol, value) { | ||
$add(alias, source, symbol, value) { | ||
if (isSpecialMethod(alias)) { | ||
@@ -87,15 +65,10 @@ return; | ||
*/ | ||
; | ||
_proto.$mock = function $mock(imports) { | ||
var _this = this; | ||
$mock(imports) { | ||
if (typeof imports === "function") { | ||
var mocks = {}; | ||
Object.keys(this.$meta).forEach(function (alias) { | ||
var _this$$meta$alias = _this.$meta[alias], | ||
source = _this$$meta$alias[0], | ||
symbol = _this$$meta$alias[1], | ||
value = _this$$meta$alias[2]; | ||
var mock = imports(source, symbol, value); | ||
const mocks = {}; | ||
Object.keys(this.$meta).forEach(alias => { | ||
const [source, symbol, value] = this.$meta[alias]; | ||
const mock = imports(source, symbol, value); | ||
@@ -111,9 +84,9 @@ if (mock != null) { | ||
Object.keys(imports).forEach(function (source) { | ||
var sourceImports = imports[source]; | ||
var esImports = sourceImports; | ||
Object.keys(imports).forEach(source => { | ||
const sourceImports = imports[source]; | ||
let esImports = sourceImports; | ||
if (typeof esImports === "function") { | ||
esImports = { | ||
"default": esImports | ||
default: esImports | ||
}; | ||
@@ -123,28 +96,22 @@ } // Handle namespace ES imports (`import * as foo from "foo"`). | ||
var namespaceAliases = Object.keys(_this.$meta).filter(function (alias) { | ||
var _this$$meta$alias2 = _this.$meta[alias], | ||
source_ = _this$$meta$alias2[0], | ||
symbol_ = _this$$meta$alias2[1]; | ||
const namespaceAliases = Object.keys(this.$meta).filter(alias => { | ||
const [source_, symbol_] = this.$meta[alias]; | ||
return source_ === source && symbol_ === "*"; | ||
}); | ||
namespaceAliases.forEach(function (alias) { | ||
_this[alias] = esImports; | ||
namespaceAliases.forEach(alias => { | ||
this[alias] = esImports; | ||
}); // Handle CJS imports (`var foo = require("bar")`). | ||
var cjsAliases = Object.keys(_this.$meta).filter(function (alias) { | ||
var _this$$meta$alias3 = _this.$meta[alias], | ||
source_ = _this$$meta$alias3[0], | ||
symbol_ = _this$$meta$alias3[1]; | ||
const cjsAliases = Object.keys(this.$meta).filter(alias => { | ||
const [source_, symbol_] = this.$meta[alias]; | ||
return source_ === source && symbol_ === "<CJS>"; | ||
}); | ||
cjsAliases.forEach(function (alias) { | ||
_this[alias] = sourceImports; | ||
cjsAliases.forEach(alias => { | ||
this[alias] = sourceImports; | ||
}); // Handle named ES imports (`import { foo } from "..."`) or | ||
// destructured CJS imports (`var { foo } = require("...")`). | ||
Object.keys(esImports).forEach(function (symbol) { | ||
var aliases = Object.keys(_this.$meta).filter(function (alias) { | ||
var _this$$meta$alias4 = _this.$meta[alias], | ||
source_ = _this$$meta$alias4[0], | ||
symbol_ = _this$$meta$alias4[1]; | ||
Object.keys(esImports).forEach(symbol => { | ||
const aliases = Object.keys(this.$meta).filter(alias => { | ||
const [source_, symbol_] = this.$meta[alias]; | ||
return source_ === source && symbol_ === symbol; | ||
@@ -154,7 +121,7 @@ }); | ||
if (aliases.length === 0 && namespaceAliases.length === 0 && cjsAliases.length === 0) { | ||
throw new Error("Module does not import \"" + symbol + "\" from \"" + source + "\""); | ||
throw new Error(`Module does not import "${symbol}" from "${source}"`); | ||
} | ||
aliases.forEach(function (alias) { | ||
_this[alias] = esImports[symbol]; | ||
aliases.forEach(alias => { | ||
this[alias] = esImports[symbol]; | ||
}); | ||
@@ -183,8 +150,6 @@ }); | ||
*/ | ||
; | ||
_proto.$restore = function $restore(imports) { | ||
var _this2 = this; | ||
Object.keys(this.$meta).forEach(function (alias) { | ||
$restore(imports) { | ||
Object.keys(this.$meta).forEach(alias => { | ||
if (isSpecialMethod(alias)) { | ||
@@ -195,26 +160,19 @@ // Skip imports which conflict with special methods. | ||
var _this2$$meta$alias = _this2.$meta[alias], | ||
source = _this2$$meta$alias[0], | ||
symbol = _this2$$meta$alias[1], | ||
value = _this2$$meta$alias[2]; | ||
var restoreMock = // `$restore()` restores all mocks. | ||
const [source, symbol, value] = this.$meta[alias]; | ||
const restoreMock = // `$restore()` restores all mocks. | ||
typeof imports === "undefined" || // `$restore({ './module': true })` restores all mocks for './module' | ||
imports[source] === true || typeof imports[source] === "object" && imports[source][symbol] === true; | ||
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; | ||
this[alias] = value; | ||
} | ||
}); | ||
}; | ||
} | ||
return ImportMap; | ||
}(); | ||
} | ||
function isSpecialMethod(name) { | ||
return ImportMap.prototype.hasOwnProperty(name); | ||
} | ||
module.exports = { | ||
ImportMap: ImportMap, | ||
MockingError: MockingError | ||
}; | ||
return Object.prototype.hasOwnProperty.call(ImportMap.prototype, name); | ||
} |
{ | ||
"name": "babel-plugin-mockable-imports", | ||
"version": "1.8.0", | ||
"version": "2.0.0", | ||
"description": "Babel plugin for mocking ES imports", | ||
"type": "module", | ||
"main": "index.js", | ||
@@ -31,13 +32,14 @@ "scripts": { | ||
"homepage": "https://github.com/robertknight/babel-plugin-mockable-imports#readme", | ||
"browserslist": "chrome >= 80, firefox >= 80, safari >= 14", | ||
"devDependencies": { | ||
"@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.2.3", | ||
"@babel/cli": "^7.16.0", | ||
"@babel/core": "^7.16.0", | ||
"@babel/plugin-syntax-jsx": "^7.16.0", | ||
"@babel/plugin-transform-destructuring": "^7.16.0", | ||
"@babel/preset-env": "^7.16.0", | ||
"chai": "^4.3.4", | ||
"eslint": "^8.1.0", | ||
"mocha": "^9.1.3", | ||
"prettier": "1.16.4" | ||
} | ||
} |
# babel-plugin-mockable-imports | ||
[![Build Status](https://travis-ci.org/robertknight/babel-plugin-mockable-imports.svg?branch=master)](https://travis-ci.org/robertknight/babel-plugin-mockable-imports) | ||
A Babel plugin that transforms JavaScript modules (CommonJS or ES2015 style) | ||
@@ -6,0 +4,0 @@ to enable mocking of their dependencies in tests. |
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
Yes
36568
569
397