babel-plugin-mockable-imports
Advanced tools
Comparing version 2.0.0 to 2.0.1
35
index.js
@@ -20,3 +20,3 @@ import * as pathModule from "path"; | ||
// internal helpers. | ||
/\0/ | ||
/\0/, | ||
]; | ||
@@ -105,3 +105,3 @@ | ||
t.stringLiteral(symbol), | ||
value | ||
value, | ||
] | ||
@@ -118,3 +118,3 @@ ) | ||
return excludeList.some( | ||
pattern => | ||
(pattern) => | ||
(typeof pattern === "string" && pattern === source) || | ||
@@ -138,3 +138,3 @@ (pattern instanceof RegExp && pattern.test(source)) | ||
const dirParts = pathModule.dirname(filename).split(pathModule.sep); | ||
return dirParts.some(part => excludeList.includes(part)); | ||
return dirParts.some((part) => excludeList.includes(part)); | ||
} | ||
@@ -186,3 +186,3 @@ | ||
source, | ||
value: id | ||
value: id, | ||
}); | ||
@@ -202,7 +202,7 @@ } else if (id.type === "ObjectPattern") { | ||
symbol, | ||
value | ||
value, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
@@ -249,3 +249,3 @@ | ||
t.identifier("ImportMap") | ||
) | ||
), | ||
], | ||
@@ -259,3 +259,3 @@ t.stringLiteral(helperImportPath) | ||
t.newExpression(t.identifier("ImportMap"), []) | ||
) | ||
), | ||
]); | ||
@@ -267,3 +267,3 @@ | ||
t.identifier("$imports") | ||
) | ||
), | ||
]); | ||
@@ -275,3 +275,4 @@ | ||
const insertedNodes = body[0].insertAfter(helperImport); | ||
insertedNodes[0].insertAfter($importsDecl); | ||
const [varPath] = insertedNodes[0].insertAfter($importsDecl); | ||
path.scope.registerDeclaration(varPath); | ||
@@ -303,3 +304,3 @@ // Insert `export { $imports }` at the end of the file. The reason for | ||
} | ||
} | ||
}, | ||
}, | ||
@@ -372,3 +373,3 @@ | ||
excludeImportsFromModules, | ||
imports | ||
imports, | ||
}); | ||
@@ -389,3 +390,3 @@ | ||
// Process import and add metadata to `state.importIdentifiers` map. | ||
path.node.specifiers.forEach(spec => { | ||
path.node.specifiers.forEach((spec) => { | ||
if (spec.local.name === "$imports") { | ||
@@ -446,3 +447,3 @@ // Abort processing the file if it declares an import called | ||
// Ignore the reference in generated `$imports.$add` calls. | ||
const callExprParent = child.findParent(p => p.isCallExpression()); | ||
const callExprParent = child.findParent((p) => p.isCallExpression()); | ||
const callee = callExprParent && callExprParent.node.callee; | ||
@@ -486,5 +487,5 @@ if ( | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-mockable-imports", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Babel plugin for mocking ES imports", | ||
@@ -39,7 +39,7 @@ "type": "module", | ||
"@babel/preset-env": "^7.16.0", | ||
"chai": "^4.3.4", | ||
"eslint": "^8.1.0", | ||
"mocha": "^9.1.3", | ||
"prettier": "1.16.4" | ||
"chai": "^4.3.6", | ||
"eslint": "^8.18.0", | ||
"mocha": "^10.0.0", | ||
"prettier": "2.7.1" | ||
} | ||
} |
# babel-plugin-mockable-imports | ||
A Babel plugin that transforms JavaScript modules (CommonJS or ES2015 style) | ||
to enable mocking of their dependencies in tests. | ||
A Babel plugin that transforms JavaScript modules to enable mocking of their dependencies in tests. | ||
@@ -12,3 +11,2 @@ See the Usage section below for information on getting started, and the FAQ at | ||
- Provides a simple interface for mocking imports in tests | ||
- Works with CommonJS (Node style) and native (ES2015) JavaScript modules | ||
- Can be used with any test runner, any bundler, and whether tests are being run | ||
@@ -112,6 +110,2 @@ under Node or in the browser | ||
If the module you want to test uses CommonJS / Node style imports instead | ||
(`var someModule = require("some-module")`, see the [section on | ||
CommonJS](#commonjs-support). | ||
See the [example project](examples/javascript) for a complete runnable project | ||
@@ -249,20 +243,2 @@ using Mocha as a test runner. | ||
### CommonJS support | ||
The plugin has basic support for CommonJS. It will recognize the | ||
following patterns as imports: | ||
```js | ||
var foo = require('./foo'); | ||
var { foo } = require('./foo'); | ||
var { foo: bar } = require('./foo'); | ||
``` | ||
Where `var` may also be `const` or `let`. If the `require` is wrapped or | ||
contained within an expression it will not be processed. | ||
When processing a CommonJS module the plugin still emits ES6 `import` and | ||
`export` declarations, so transforming of ES6 `import`/`export` statements | ||
to CommonJS must be enabled in Babel. | ||
## Usage with TypeScript | ||
@@ -351,3 +327,3 @@ | ||
There is currently no support for dynamic imports, either using `import()` to obtain a promise for a module, or calling `require` anywhere other than at the top level of a module. | ||
There is currently no support for dynamic imports using `import()`. | ||
@@ -354,0 +330,0 @@ ## Troubleshooting |
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
570
35730
373