Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 20, 2018
1 parent bdc4883 commit e7adbf3
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions scripts/babel-plugin-jest-native-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
// This plugin exists to make sure that we use a `Promise` that has not been messed with by user code.
// Might consider extending this to other globals as well in the future

const jestPromise =
"(global[Symbol.for('jest-native-promise')] || global.Promise)";
module.exports = ({template}) => {
const promiseDeclaration = template(`
var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
`);

module.exports = () => ({
name: 'jest-native-promise',
visitor: {
MemberExpression(path) {
if (path.node.object.name === 'Promise') {
path.node.object.name = jestPromise;
}
return {
name: 'jest-native-promise',
visitor: {
ReferencedIdentifier(path, state) {
if (path.node.name === 'Promise' && !state.injectedPromise) {
state.injectedPromise = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', promiseDeclaration());
}
},
},
NewExpression(path) {
if (path.node.callee.name === 'Promise') {
path.node.callee.name = jestPromise;
}
},
},
});
};
};

0 comments on commit e7adbf3

Please sign in to comment.