forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
188 lines (172 loc) · 4.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* 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
import {NodePath, Visitor} from '@babel/traverse';
// eslint-disable-next-line
import {Identifier} from '@babel/types';
const invariant = (condition: unknown, message: string) => {
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: Set<string> = new Set([
'Array',
'ArrayBuffer',
'Boolean',
'BigInt',
'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: NodePath<Identifier>) {
// @ts-ignore: passed as Visitor State
this.ids.add(path);
},
blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference'],
};
const FUNCTIONS: {
[key: string]: (args: Array<NodePath>) => boolean;
} = Object.create(null);
FUNCTIONS.mock = (args: Array<NodePath>) => {
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: Set<NodePath<Identifier>> = new Set();
const parentScope = moduleFactory.parentPath.scope;
// @ts-ignore: Same as above: ReferencedIdentifier doesn't exist
moduleFactory.traverse(IDVisitor, {ids});
for (const id of ids) {
const {name} = id.node;
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.',
);
}
}
return true;
}
return false;
};
FUNCTIONS.unmock = (args: Array<NodePath>) =>
args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.deepUnmock = (args: Array<NodePath>) =>
args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = (
args: Array<NodePath>,
) => args.length === 0;
export = () => {
const shouldHoistExpression = (expr: NodePath): boolean => {
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') as NodePath;
const property = callee.get('property') as NodePath;
return (
property.isIdentifier() &&
FUNCTIONS[property.node.name] &&
(object.isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
FUNCTIONS[property.node.name](expressionArguments)
);
};
const visitor: Visitor = {
ExpressionStatement(path) {
if (shouldHoistExpression(path.get('expression'))) {
// @ts-ignore: private, magical property
path.node._blockHoist = Infinity;
}
},
};
return {visitor};
};