-
-
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
🐛 Bug: Module
s in errors cause unhandledRejection, tests quit early
#4887
Comments
@JacobLey I can't figure out how to reproduce this locally. I believe you that it is a real bug 😄 but need an isolated reproduction to test it with. What's the simplest way to get this to happen in a clean/new repository? For reference, trying this with import { expect } from 'chai';
describe('Compare imports', () => {
it('different imports', async () => {
const modA = await import('dedent');
const modB = await import('once');
// throws
expect(modA).to.eq(modB);
});
it('always true', () => {
console.log('This will never run');
});
}); ...I get fine looking failures:
Same with switching to |
Module
s in errors cause unhandledRejection, tests quit earlyModule
s in errors cause unhandledRejection, tests quit early
I've finally gotten around to trying to reproduce! Sorry there was a minor mistake in my example. Change the code to instead be
Now the module is not the "top level" object and experiences the issue. So when stringify-ing the error, https://github.com/mochajs/mocha/blob/master/lib/utils.js#L242 this will attempt the "canonicalize" the top-level Which internally runs this loop over the keys and tries to "implicitly stringify" the module: https://github.com/mochajs/mocha/blob/master/lib/utils.js#L408 The error didn't show up in my original example, because Mocha correctly handles "Module" in that case. Not using the suggested approach above, but handles it nonetheless. In that case it jumps straight to the json stringification https://github.com/mochajs/mocha/blob/master/lib/utils.js#L235 Which falls back on native JSON.stringify https://github.com/mochajs/mocha/blob/master/lib/utils.js#L315 which works Looking back at my original suggested solution, it should resolve the issue: https://github.com/mochajs/mocha/pull/5040/files |
Did a little bit more research and actually think proposed solution does not go far enough... Basically the two curveballs that are happening are:
So you could actually recreate this with any instance that fulfills those requirements. import { expect } from 'chai';
import { describe, it } from 'mocha';
describe('Compare imports', () => {
it('different imports', async () => {
const foo = Object.create(null, {
[Symbol.toStringTag]: { value: 'Foo' },
bing: { get: () => 'bong', enumerable: true }
});
const bar = Object.create(null, {
[Symbol.toStringTag]: { value: 'Bar' },
bing: { get: () => 'boing', enumerable: true }
});
// throws
expect({ a: foo }).to.deep.equal({ b: bar });
});
it('always true', () => {
console.log('This will never run');
});
}); Similar to above, Updated PR with comments as well. |
Easily reproducible version for anyone that wants to test out: https://github.com/JacobLey/issue-recreator/tree/mocha-esm |
Thanks, confirmed that that reproduces locally for me. 🚀 |
Kind of reminds me of nodejs/node#48918 (comment), but probably two different issues |
Released in |
Prerequisites
faq
labelnode_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
When a mocha test throws an error that contains a
Module
Mocha will quit the process unexpectedly.Internally this is caused by Mocha being unable to serialize ("canonicalize") the module object, and throwing an error.
Steps to Reproduce
Simple test:
Reproduces how often: 100%
Versions
mocha --version
=>10.0.0
node --version
=>v16.15.0
Additional Information
Will open PR to fix.
canonicalType
returning'module'
for modules (I agree with this behavior)canonicalize
does not handle the'module'
case explicitly, so it falls back on implicit stringificationimport('fs').then(fs => fs + '')
=>Uncaught TypeError: Cannot convert object to primitive value
)The text was updated successfully, but these errors were encountered: