babel-plugin-jest-hoist
Advanced tools
Comparing version 10.0.1 to 10.0.2
{ | ||
"name": "babel-plugin-jest-hoist", | ||
"version": "10.0.1", | ||
"version": "10.0.2", | ||
"repository": { | ||
@@ -11,3 +11,2 @@ "type": "git", | ||
"devDependencies": { | ||
"babel-jest": "*", | ||
"babel-preset-es2015": "*", | ||
@@ -17,3 +16,4 @@ "react": "^0.14.0" | ||
"jest": { | ||
"testEnvironment": "<rootDir>/../../src/environments/NodeEnvironment" | ||
"scriptPreprocessor": "../babel-jest", | ||
"testEnvironment": "../jest-environment-node" | ||
}, | ||
@@ -20,0 +20,0 @@ "scripts": { |
@@ -52,2 +52,9 @@ /** | ||
describe('babel-plugin-jest-hoist', () => { | ||
it('does not throw during transform', () => { | ||
const object = {}; | ||
object.__defineGetter__('foo', () => 'bar'); | ||
expect(object.foo).toEqual('bar'); | ||
}); | ||
it('hoists react unmock call before imports', () => { | ||
@@ -54,0 +61,0 @@ expect(typeof React).toEqual('object'); |
@@ -76,51 +76,53 @@ /** | ||
const FUNCTIONS = { | ||
mock: args => { | ||
if (args.length === 1) { | ||
return args[0].isStringLiteral(); | ||
} else if (args.length === 2) { | ||
const moduleFactory = args[1]; | ||
invariant( | ||
moduleFactory.isFunction(), | ||
'The second argument of `jest.mock` must be a function.' | ||
); | ||
const FUNCTIONS = Object.create(null); | ||
FUNCTIONS.mock = args => { | ||
if (args.length === 1) { | ||
return args[0].isStringLiteral(); | ||
} else if (args.length === 2) { | ||
const moduleFactory = args[1]; | ||
invariant( | ||
moduleFactory.isFunction(), | ||
'The second argument of `jest.mock` must be a function.' | ||
); | ||
const ids = new Set(); | ||
const parentScope = moduleFactory.parentPath.scope; | ||
moduleFactory.traverse(IDVisitor, {ids}); | ||
for (const id of ids) { | ||
const name = id.node.name; | ||
let found = false; | ||
let scope = id.scope; | ||
const ids = new Set(); | ||
const parentScope = moduleFactory.parentPath.scope; | ||
moduleFactory.traverse(IDVisitor, {ids}); | ||
for (const id of ids) { | ||
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; | ||
while (scope !== parentScope) { | ||
if (scope.bindings[name]) { | ||
found = true; | ||
break; | ||
} | ||
if (!found) { | ||
invariant( | ||
scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name], | ||
'The second argument of `jest.mock()` is not allowed to ' + | ||
'reference any outside variables.\n' + | ||
'Invalid variable access: ' + name + '\n' + | ||
'Whitelisted objects: ' + | ||
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.' | ||
); | ||
} | ||
scope = scope.parent; | ||
} | ||
return true; | ||
if (!found) { | ||
invariant( | ||
scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name], | ||
'The second argument of `jest.mock()` is not allowed to ' + | ||
'reference any outside variables.\n' + | ||
'Invalid variable access: ' + name + '\n' + | ||
'Whitelisted objects: ' + | ||
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.' | ||
); | ||
} | ||
} | ||
return false; | ||
}, | ||
unmock: args => args.length === 1 && args[0].isStringLiteral(), | ||
disableAutomock: args => args.length === 0, | ||
enableAutomock: args => args.length === 0, | ||
return true; | ||
} | ||
return false; | ||
}; | ||
FUNCTIONS.unmock = args => args.length === 1 && args[0].isStringLiteral(); | ||
FUNCTIONS.disableAutomock = | ||
FUNCTIONS.enableAutomock = | ||
args => args.length === 0; | ||
module.exports = babel => { | ||
@@ -127,0 +129,0 @@ const shouldHoistExpression = expr => { |
11255
2
320