Skip to content

Commit de1b459

Browse files
cpojerFacebook Github Bot 0
authored andcommitted
Fix jest-hoist for react-native.
Summary:Because the Object in the hoist transform had `Object.prototype` as its prototype, lookups like `FUNCTIONS[foo]` will break for some keys `foo` like `__defineGetter__`. This fixes it. Closes jestjs#864 Reviewed By: kentaromiura Differential Revision: D3138430 fb-gh-sync-id: c8f5d55c0711af7f2cd9d5266be84f4db8267bb0 fbshipit-source-id: c8f5d55c0711af7f2cd9d5266be84f4db8267bb0
1 parent 94c7e3f commit de1b459

File tree

4 files changed

+51
-41
lines changed

4 files changed

+51
-41
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.haste_cache
22
__tests__
3+
blog
34
docs
45
examples
56
packages

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jest-cli",
33
"description": "Painless JavaScript Unit Testing.",
4-
"version": "0.10.0",
4+
"version": "0.10.1",
55
"main": "src/jest.js",
66
"dependencies": {
77
"chalk": "^1.1.1",

packages/babel-plugin-jest-hoist/src/__tests__/integration-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ jest.unmock('../__test_modules__/' + 'c');
4949
jest.dontMock('../__test_modules__/Mocked');
5050

5151
describe('babel-plugin-jest-hoist', () => {
52+
53+
it('does not throw during transform', () => {
54+
const object = {};
55+
object.__defineGetter__('foo', () => 'bar');
56+
expect(object.foo).toEqual('bar');
57+
});
58+
5259
it('hoists react unmock call before imports', () => {
5360
expect(typeof React).toEqual('object');
5461
expect(React.isValidElement.mock).toBe(undefined);

packages/babel-plugin-jest-hoist/src/index.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -73,55 +73,57 @@ const IDVisitor = {
7373
},
7474
};
7575

76-
const FUNCTIONS = {
77-
mock: args => {
78-
if (args.length === 1) {
79-
return args[0].isStringLiteral();
80-
} else if (args.length === 2) {
81-
const moduleFactory = args[1];
82-
invariant(
83-
moduleFactory.isFunction(),
84-
'The second argument of `jest.mock` must be a function.'
85-
);
86-
87-
const ids = new Set();
88-
const parentScope = moduleFactory.parentPath.scope;
89-
moduleFactory.traverse(IDVisitor, {ids});
90-
for (const id of ids) {
91-
const name = id.node.name;
92-
let found = false;
93-
let scope = id.scope;
76+
const FUNCTIONS = Object.create(null);
77+
FUNCTIONS.mock = args => {
78+
if (args.length === 1) {
79+
return args[0].isStringLiteral();
80+
} else if (args.length === 2) {
81+
const moduleFactory = args[1];
82+
invariant(
83+
moduleFactory.isFunction(),
84+
'The second argument of `jest.mock` must be a function.'
85+
);
9486

95-
while (scope !== parentScope) {
96-
if (scope.bindings[name]) {
97-
found = true;
98-
break;
99-
}
87+
const ids = new Set();
88+
const parentScope = moduleFactory.parentPath.scope;
89+
moduleFactory.traverse(IDVisitor, {ids});
90+
for (const id of ids) {
91+
const name = id.node.name;
92+
let found = false;
93+
let scope = id.scope;
10094

101-
scope = scope.parent;
95+
while (scope !== parentScope) {
96+
if (scope.bindings[name]) {
97+
found = true;
98+
break;
10299
}
103100

104-
if (!found) {
105-
invariant(
106-
scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name],
107-
'The second argument of `jest.mock()` is not allowed to ' +
108-
'reference any outside variables.\n' +
109-
'Invalid variable access: ' + name + '\n' +
110-
'Whitelisted objects: ' +
111-
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.'
112-
);
113-
}
101+
scope = scope.parent;
114102
}
115103

116-
return true;
104+
if (!found) {
105+
invariant(
106+
scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name],
107+
'The second argument of `jest.mock()` is not allowed to ' +
108+
'reference any outside variables.\n' +
109+
'Invalid variable access: ' + name + '\n' +
110+
'Whitelisted objects: ' +
111+
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.'
112+
);
113+
}
117114
}
118-
return false;
119-
},
120-
unmock: args => args.length === 1 && args[0].isStringLiteral(),
121-
disableAutomock: args => args.length === 0,
122-
enableAutomock: args => args.length === 0,
115+
116+
return true;
117+
}
118+
return false;
123119
};
124120

121+
FUNCTIONS.unmock = args => args.length === 1 && args[0].isStringLiteral();
122+
123+
FUNCTIONS.disableAutomock =
124+
FUNCTIONS.enableAutomock =
125+
args => args.length === 0;
126+
125127
module.exports = babel => {
126128
const shouldHoistExpression = expr => {
127129
if (!expr.isCallExpression()) {

0 commit comments

Comments
 (0)