mocha icon indicating copy to clipboard operation
mocha copied to clipboard

🐛 Bug: Mocha loads incomplete Promise polyfill

Open straker opened this issue 2 years ago • 4 comments

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

I recently upgrade Mocha from v7 to latest and noticed that Mocha loads its own Promise polyfill. Unfortunately the polyfill is incomplete and does not implement Promise.finally and causes our tests to fail in IE11 (sorry, we can't drop support yet). We try to load es6-promise if we detect that Promise isn't already defined on window, but since Mocha loads first we detect that Promise exists and don't load our working polyfill.

I verified that the Promise polyfill is loaded starting in v8.1.0 and above.

With mocha on the page image

Without mocha on the page image

straker avatar Nov 02 '21 20:11 straker

@straker is the first image from IE11? Showing "F12 Developer Tools" and "function Promise() { [native code] }?

Anyway I haven't reproduced your issue, because it's IE11 ... In our Babel configuration we set useBuiltIns: 'usage', therefore only those polyfills are inserted which we are using in our Mocha code. And we haven't used Promise#finally() so far. For testing I inserted a dummy finally() somewhere in runner.js and now the debug log of the build shows - besides es.promise - an additional es.promise.finally polyfill (both from core-js).

So I don't know how to proceed:

  • should Mocha add es.promise.finally, via a dummy finally() or evtl. via the include option?
  • could you check for window.Promise and window.Promise.finally and load your own polyfill?

juergba avatar Nov 04 '21 13:11 juergba

see also #4446

juergba avatar Nov 04 '21 13:11 juergba

Thanks for the information. Yes the first image is from IE11 when mocha is loaded on the page. I'm not sure why core-js adds Promise as [native code], but it seems to.

Would it be possible import the core-js Promise polyfill in the mocha code where it's used and then use the non-global import? That might get around the global pollute.

import Promise from 'core-js-pure/features/promise';

straker avatar Nov 04 '21 14:11 straker

I don't know, but by fixing "global Promise" only, the root problem still persists. There are dozens of other pollyfills loaded by Mocha.

There is this babel-polyfills package, which seems to offer an option "method": "usage-pure" in order to prevent global pollution. I think it relies on core-js-pure.

I would need some help on this issue. Maybe you know somebody, who could struggle through this transpiling challenge. We also have some funds to spend.

juergba avatar Nov 04 '21 15:11 juergba