-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix EvalError caused by regenerator-runtime (#4535) #4577
Conversation
@snoack thanks for this PR! The browser tests never work on PR's from forked repos. This is a problem of secrets (saucelabs login info) not being shared with forked repos. I'm going to push your branch to Mocha's repo directly. Edit: browser tests are passing now. |
@Munter I need your review for this PR, please. |
@snoack have you tested this new bundle in your environment? |
Yes, I did. Works. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snoack thank you!
Please be patient for some days, I will wait for more reviews.
Babel seems to struggle a bit with publishing, see Babel v7.12.18. |
@snoack I'm going to merge this branch tomorrow. Babel is publishing new releases every few hours, some kind of weird. I don't think we need to upgrade to the newest one, what do you think? |
Sounds good. |
Description of the Change
Mocha provides a bundle that can be used directly in the browser. For compatibility with Internet Explorer 11, the bundled code is transpiled using Babel. When using
async
functions (or generator functions) as Mocha started in 12db9db (8.2.0), Babel adds theregenerator-runtime
module to the bundle, which relies on unsafe eval to access the global scope in strict mode, which in turn makes Mocha incompatible with environments that don't allow unsafe eval.In this PR, I implement a workaround that prevents
regenerator-runtime
from using unsafe eval. First I had to updateregenerator-runtime
since the currently used version relies onthis
in an unbound function referring to the global object which we don't have any control over, while the new version instead exploits implicit global assignments:While this isn't working either in strict mode without unsafe eval, it allows us to work around the issue by declaring the
regeneratorRuntime
variable before this code is reached (done here via Rollup'soutput.intro
setting). That wayregeneratorRuntime = runtime
is no longer an implicit definition of a global variable (which fails in strict mode), but it becomes a simple reassignment of an existing variable, and so the code in thecatch
block (which requires unsafe eval) is never reached.Alternate Designs
transform-regenerator
in which case the transpiled code would at least remain compatible with legacy browsers that support generator functions but might lack other modern ES features.async
/await
.Alternative 1 and 2 were ruled out by @juergba in the discussion in #4535. Alternative 3 seems somewhat more complicated than the workaround I ended up implementing here, and it would require users to choose the correct bundle for their environment.
Why should this be in core?
Because it fixes a regression introduced in Mocha 3.2.0.
Benefits
Mocha can be used once again in environments where unsafe eval is not allowed. This includes browser extensions, as well as websites with restrictive Content-Security-Policy.