unhelpful error messages while using asynch in before() #4850
Description
Prerequisites
- 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_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.
Description
Being a noob with ESM, I did not realize that I could not simply follow the documentation at the top of https://mochajs.org/. After following the "prerequisites" steps for filing a ticket, I found my error and eventually found https://mochajs.org/#nodejs-native-esm-support. However, I believe I had some unneeded frustration. The error message I got was:
SyntaxError[ @/Users/jric/dev/socialvoter/src/server/js/test/test.js ]: Unexpected reserved word
at Loader.moduleStrategy (node:internal/modules/esm/translators:146:18)
at async link (node:internal/modules/esm/module_job:67:21)
for which I found very little actionable information, mostly in this ticket: nodejs/node#49441
Steps to Reproduce
create package.json
with "type"="module"
create this test file, and run it with mocha
var assert = require('assert');
function myAsyncFunction() { return new Promise(() => {}, () => {}); }
describe('Server', function() {
before(function() {
await myAsyncFunction();
});
});
Expected behavior:
I expect to see an error, such as I get when I remove the "await" from the test code above.
ReferenceError: require is not defined in ES module scope, you can use import instead
As another simple fix, I expect to see a qualification at the top of the documentation that specifies that the sample code is only for using the common module system, and it could link down to the alternate code for using ESM.
Actual behavior:
As mentioned above, I dutifully follow the sample code and get a very cryptic error.
Reproduces how often: 100%
Versions
- The output of
mocha --version
andnode_modules/.bin/mocha --version
:
jric@Alexs-MacBook-Pro-3 js % mocha --version
zsh: command not found: mocha
jric@Alexs-MacBook-Pro-3 js % node_modules/.bin/mocha --version
9.2.1
- The output of
node --version
:
jric@Alexs-MacBook-Pro-3 js % node --version
v16.6.1
- Your operating system
- name and version:
macOS Monterey 12.2.1
- architecture (32 or 64-bit):
64 bit
- name and version:
- Your shell (e.g., bash, zsh, PowerShell, cmd):
zsh
- Your browser and version (if running browser tests): N/A
- Any third-party Mocha-related modules (and their versions): N/A
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): N/A
Additional Information
N/A
Activity