-
-
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
Unable to import mocha API functions from 'mocha' #4763
Comments
@PaperStrike can you join in, please? I need your support. @apellerano-pw so you are bundling using Mocha's bundle, or using browser-entry.js only? If you evtl. have some time left, could you test which solution would work in your case: |
Hello @juergba, thank you for reaching out. I'm not fully aware of the bundling approach because I'm using karma along with karma-mocha so it is normally abstracted away. But I have breakpointed the issue and it looks to be importing browser-entry.js. Here is the failing line in the bundle: (0,mocha__WEBPACK_IMPORTED_MODULE_0__.beforeEach)(function () { And here is the earlier definition of that var: /* harmony import */ var mocha__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! mocha */ "./node_modules/mocha/browser-entry.js"); I also looked at the bundled output for browser-entry.js and its Mocha import is mocha.js. var Mocha = __webpack_require__(/*! ./lib/mocha */ "./node_modules/mocha/lib/mocha.js"); I didn't find mocha-es2018.js in the bundle. I locally modified my browser-entry.js to include the snippet from your first linked comment (iterate over I next tried the second linked comment (push a hardcoded array of props from The arrays are quite different between them so that could explain the difference in behavior. As could the source of the function. The non-working solution copies them from Object.keys(Mocha).filter(k => !(k in mocha)).sort()
> (23) ['Context', 'Hook', 'Runnable', 'Runner', 'Suite', 'Test', 'after', 'afterEach', 'before', 'beforeEach', 'describe', 'interfaces', 'it', 'process', 'reporters', 'suiteSetup', 'suiteTeardown', 'teardown', 'test', 'unloadFile', 'utils', 'xdescribe', 'xit']
[ ... the manual array ].sort()
> (21) ['Mocha', 'after', 'afterEach', 'before', 'beforeEach', 'context', 'describe', 'it', 'mocha', 'run', 'setup', 'specify', 'suite', 'suiteSetup', 'suiteTeardown', 'teardown', 'test', 'xcontext', 'xdescribe', 'xit', 'xspecify'] |
I'm willing to join in but I'm quite confused how it works too. Sorry for the inconvenience. I never have the global |
@apellerano-pw @PaperStrike thank you, guys. In Node this I tend to the second solution. It partially does what we had before, assigning from Some months ago we bundled to IIFE format, then had to switched to UMD due to problems with other bundlers using our bundle. |
Do you both agree with the second approach? |
Second approach makes the most sense to me. |
Emm, I'm still confused how you can have unusable And if any of you are willing to help me through it, here's my question:
That hook is fired here, Lines 137 to 141 in 111467f
and Lines 147 to 165 in 111467f
Considering the second approach works, it seems like one can have valid |
In the
We don't know either how karma/karma-mocha is bundling there bundle. It looks like they don't use Mocha's bundle, but create their own, see |
My latest comment doesn't really help. What about moving the function copying into the Or could an additional, second |
There must be more than one bundling process, with karma-runner and also with our own CI tests. The first one bundles |
@apellerano-pw could you test with this short patch, please? // prettier-ignore
[
'describe', 'it', 'before', 'beforeEach', 'afterEach', 'after',
'xdescribe', 'xit'
].forEach(function(key) {
mocha[key] = global[key];
}); If this patch works, I will open a PR and merge, but for sure it's not a nice solution, and it has never been:
|
Thank you for suggesting a patch, @juergba. I tested the following patch and it works. [
'after',
'afterEach',
'before',
'beforeEach',
'context',
'describe',
'it',
'specify',
'xcontext',
'xdescribe',
'xit',
'xspecify'
].forEach(function (key) {
mocha[key] = global[key];
}); I added the |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
It is no longer possible to do ES6 style imports from mocha. e.g.
Steps to Reproduce
Write a my-test.js module like
Expected behavior:
This test runs and passes
Actual behavior:
Something like
(In this case webpack is the bundler)
Reproduces how often:
100%
Versions
I believe this was introduced by this PR: #4746 and released as version 9.1.2
Additional info
I agree with the general goal in the PR: to stop polluting the
mocha
module with the contents ofglobal
; but it throws the baby out with the bathwater because this was previously how you could write self-documenting import statements which keeps linters happy.eslint:recommended
containsno-undef
so if you are writing your tests in modules and bundling them together, you are now in a pickle.The text was updated successfully, but these errors were encountered: