This commit is contained in:
eric sciple 2020-01-24 12:20:47 -05:00
parent a004f0ae58
commit fc725ba36b
7280 changed files with 19 additions and 1796407 deletions

View file

@ -1,23 +0,0 @@
MIT License
For Jest software
Copyright (c) 2014-present, Facebook, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,33 +0,0 @@
# babel-plugin-jest-hoist
Babel plugin to hoist `jest.disableAutomock`, `jest.enableAutomock`, `jest.unmock`, `jest.mock`, calls above `import` statements. This plugin is automatically included when using [babel-jest](https://github.com/facebook/jest/tree/master/packages/babel-jest).
## Installation
```sh
$ yarn add --dev babel-plugin-jest-hoist
```
## Usage
### Via `babel.config.js` (Recommended)
```js
module.exports = {
plugins: ['jest-hoist'],
};
```
### Via CLI
```sh
$ babel --plugins jest-hoist script.js
```
### Via Node API
```javascript
require('@babel/core').transform('code', {
plugins: ['jest-hoist'],
});
```

View file

@ -1,13 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import { Visitor } from '@babel/traverse';
declare const _default: () => {
visitor: Visitor<{}>;
};
export = _default;
//# sourceMappingURL=index.d.ts.map

View file

@ -1 +0,0 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAW,OAAO,EAAC,MAAM,iBAAiB,CAAC;;;;AAkJlD,kBA8BE"}

View file

@ -1,208 +0,0 @@
'use strict';
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
// Only used for types
// eslint-disable-next-line
// eslint-disable-next-line
const invariant = (condition, message) => {
if (!condition) {
throw new Error('babel-plugin-jest-hoist: ' + message);
}
}; // We allow `jest`, `expect`, `require`, all default Node.js globals and all
// ES2015 built-ins to be used inside of a `jest.mock` factory.
// We also allow variables prefixed with `mock` as an escape-hatch.
const WHITELISTED_IDENTIFIERS = new Set([
'Array',
'ArrayBuffer',
'Boolean',
'DataView',
'Date',
'Error',
'EvalError',
'Float32Array',
'Float64Array',
'Function',
'Generator',
'GeneratorFunction',
'Infinity',
'Int16Array',
'Int32Array',
'Int8Array',
'InternalError',
'Intl',
'JSON',
'Map',
'Math',
'NaN',
'Number',
'Object',
'Promise',
'Proxy',
'RangeError',
'ReferenceError',
'Reflect',
'RegExp',
'Set',
'String',
'Symbol',
'SyntaxError',
'TypeError',
'URIError',
'Uint16Array',
'Uint32Array',
'Uint8Array',
'Uint8ClampedArray',
'WeakMap',
'WeakSet',
'arguments',
'console',
'expect',
'isNaN',
'jest',
'parseFloat',
'parseInt',
'require',
'undefined'
]);
Object.keys(global).forEach(name => {
WHITELISTED_IDENTIFIERS.add(name);
});
const JEST_GLOBAL = {
name: 'jest'
}; // TODO: Should be Visitor<{ids: Set<NodePath<Identifier>>}>, but `ReferencedIdentifier` doesn't exist
const IDVisitor = {
ReferencedIdentifier(path) {
// @ts-ignore: passed as Visitor State
this.ids.add(path);
},
blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference']
};
const FUNCTIONS = Object.create(null);
FUNCTIONS.mock = args => {
if (args.length === 1) {
return args[0].isStringLiteral() || args[0].isLiteral();
} else if (args.length === 2 || args.length === 3) {
const moduleFactory = args[1];
invariant(
moduleFactory.isFunction(),
'The second argument of `jest.mock` must be an inline function.'
);
const ids = new Set();
const parentScope = moduleFactory.parentPath.scope; // @ts-ignore: Same as above: ReferencedIdentifier doesn't exist
moduleFactory.traverse(IDVisitor, {
ids
});
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (
var _iterator = ids[Symbol.iterator](), _step;
!(_iteratorNormalCompletion = (_step = _iterator.next()).done);
_iteratorNormalCompletion = true
) {
const id = _step.value;
const name = id.node.name;
let found = false;
let scope = id.scope;
while (scope !== parentScope) {
if (scope.bindings[name]) {
found = true;
break;
}
scope = scope.parent;
}
if (!found) {
invariant(
(scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS.has(name)) ||
/^mock/i.test(name) || // Allow istanbul's coverage variable to pass.
/^(?:__)?cov/.test(name),
'The module factory of `jest.mock()` is not allowed to ' +
'reference any out-of-scope variables.\n' +
'Invalid variable access: ' +
name +
'\n' +
'Whitelisted objects: ' +
Array.from(WHITELISTED_IDENTIFIERS).join(', ') +
'.\n' +
'Note: This is a precaution to guard against uninitialized mock ' +
'variables. If it is ensured that the mock is required lazily, ' +
'variable names prefixed with `mock` (case insensitive) are permitted.'
);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return true;
}
return false;
};
FUNCTIONS.unmock = args => args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.deepUnmock = args => args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = args =>
args.length === 0;
module.exports = () => {
const shouldHoistExpression = expr => {
if (!expr.isCallExpression()) {
return false;
}
const callee = expr.get('callee');
const expressionArguments = expr.get('arguments'); // TODO: avoid type casts - the types can be arrays (is it possible to ignore that without casting?)
const object = callee.get('object');
const property = callee.get('property');
return (
property.isIdentifier() &&
FUNCTIONS[property.node.name] &&
(object.isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
FUNCTIONS[property.node.name](expressionArguments)
);
};
const visitor = {
ExpressionStatement(path) {
if (shouldHoistExpression(path.get('expression'))) {
// @ts-ignore: private, magical property
path.node._blockHoist = Infinity;
}
}
};
return {
visitor
};
};

View file

@ -1,59 +0,0 @@
{
"_args": [
[
"babel-plugin-jest-hoist@24.6.0",
"/Users/eric/repos/actions/setup-node"
]
],
"_development": true,
"_from": "babel-plugin-jest-hoist@24.6.0",
"_id": "babel-plugin-jest-hoist@24.6.0",
"_inBundle": false,
"_integrity": "sha512-3pKNH6hMt9SbOv0F3WVmy5CWQ4uogS3k0GY5XLyQHJ9EGpAT9XWkFd2ZiXXtkwFHdAHa5j7w7kfxSP5lAIwu7w==",
"_location": "/babel-plugin-jest-hoist",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "babel-plugin-jest-hoist@24.6.0",
"name": "babel-plugin-jest-hoist",
"escapedName": "babel-plugin-jest-hoist",
"rawSpec": "24.6.0",
"saveSpec": null,
"fetchSpec": "24.6.0"
},
"_requiredBy": [
"/babel-preset-jest"
],
"_resolved": "https://registry.npmjs.org/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-24.6.0.tgz",
"_spec": "24.6.0",
"_where": "/Users/eric/repos/actions/setup-node",
"bugs": {
"url": "https://github.com/facebook/jest/issues"
},
"dependencies": {
"@types/babel__traverse": "^7.0.6"
},
"description": "Babel plugin to hoist `jest.disableAutomock`, `jest.enableAutomock`, `jest.unmock`, `jest.mock`, calls above `import` statements. This plugin is automatically included when using [babel-jest](https://github.com/facebook/jest/tree/master/packages/babel-jest).",
"devDependencies": {
"@babel/types": "^7.3.3"
},
"engines": {
"node": ">= 6"
},
"gitHead": "04e6a66d2ba8b18bee080bb28547db74a255d2c7",
"homepage": "https://github.com/facebook/jest#readme",
"license": "MIT",
"main": "build/index.js",
"name": "babel-plugin-jest-hoist",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/facebook/jest.git",
"directory": "packages/babel-plugin-jest-hoist"
},
"types": "build/index.d.ts",
"version": "24.6.0"
}

View file

@ -1,7 +0,0 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
}