Unable to import mocha API functions from 'mocha'
See original GitHub issuePrerequisites
- Checked that your issue hasn’t already been filed by cross-referencing issues with the
faq
label - 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.
- ‘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
- 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) 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.
import { beforeEach, afterEach } from 'mocha';
import { describe, it } from 'mocha';
Steps to Reproduce
Write a my-test.js module like
import { describe, it } from 'mocha';
describe('A test', function () {
it('should thing', function () {
});
});
Expected behavior:
This test runs and passes
Actual behavior:
Something like
TypeError: (0 , mocha__WEBPACK_IMPORTED_MODULE_2__.describe) is not a function
(In this case webpack is the bundler)
Reproduces how often:
100%
Versions
I believe this was introduced by this PR: https://github.com/mochajs/mocha/pull/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 of global
; 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
contains no-undef
so if you are writing your tests in modules and bundling them together, you are now in a pickle.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (9 by maintainers)
Top Results From Across the Web
Mocha + TypeScript: Cannot use import statement outside a ...
I created the app using TypeScript and CommonJS module to transpile, so I added "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' ...
Read more >Parallel mode fails silently sometimes with tests that import ...
Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.
Read more >mocha-loader | webpack - JS.ORG
Allows Mocha tests to be loaded and run via webpack. ... To begin, you'll need to install mocha-loader and mocha : ... import...
Read more >Documentation - Mocha
Manually dispose this mocha instance. Mark this instance as disposed and unable to run more tests. It also removes function references to tests...
Read more >Mocha documentation — DevDocs
This is useful if the APIs you are testing return promises instead of taking callbacks: beforeEach(function() { return db.clear ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@apellerano-pw @PaperStrike thank you, guys.
In Node this
currentContext
is set toglobal
(which corresponds toglobalThis
/window
/ ?? after bundling). We use a proxy for settingcurrentContext
since in parallel mode each process has its ownglobal
. In browser there is no parallel mode, so evtl. by just addingvar currentContext = global;
to browser-entry.js should do the trick.I tend to the second solution. It partially does what we had before, assigning from
global
tomocha
, this time just more selective.Some months ago we bundled to IIFE format, then had to switched to UMD due to problems with other bundlers using our bundle.
Mocha
,mocha
, andglobal
are not identical in these two formats. So - when we drop IE11 with next major release - I’m dreaming of bundling to the ESM format. Which hopefully should be easier to understand and also capable for other bundlers.Thank you for suggesting a patch, @juergba. I tested the following patch and it works.
I added the
context
andspecify
(andxcontext
,xspecify
) entries to the array since they are synonyms fordescribe
andit
respectively.