Skip to content
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

Mocha 2.3.4: Errors That Occur Outside of beforeEach/it in an AMD Module Lose Stack Trace #2167

Closed
machineghost opened this issue Mar 25, 2016 · 4 comments

Comments

@machineghost
Copy link

Short Version: If an error occurs inside a Mocha function inside an AMD module, but not inside a Mocha function that expects errors (eg. it), the stack trace of the error somehow gets eaten.

Reproduction Steps:

Step 1: npm install mocha
Step 2: create testMain.js (ie. a file which does nothing but use Require.js to import testFoo.js):

requirejs = require('node-requirejs').requirejs();
requirejs('testFoo');

Step 3: create testFoo.js (ie. a simple test file with errors thrown in unexpected places)

define([], function() {
    throw new Error('a: no trace');
    describe('Foo', () => {
        throw new Error('b: no trace');

        it('does bar', () => {
            throw new Error('c: has stack trace');
        });
    });
});

Step 4: run mocha testMain.js; you will get the following output (without a stack trace):

[Error: a: no trace]

NOTE 1: The stack trace is also lost if you comment out error "a" and make it to error "b"
NOTE 2: The stack trace is NOT lost if you comment out the first two errors and make it to error "c".
NOTE 3: The stack trace is also not lost if you throw a new error before the mocha code starts (ie. if you throw new Error('d') before the define).

Obviously it's very hard to debug a problem when this happens (you have to resort to "delete debugging", ie. removing random chunks of modified code until the error goes away), so if there's any way Mocha could provide a stack trace under these conditions it would be very helpful.

P.S. I suppose one could argue that this is a Require.js problem, not a Mocha problem. However, the fact that everything works as expected outside Mocha in an AMD environment, or inside an AMD environment inside an it/after/beforeEach/etc., but not inside the other Mocha functions, suggests that Mocha is doing something in those functions that's the root cause of the issue (even though Requirejs/AMD does seem to be a key component).

@drazisil drazisil added type: bug a defect, confirmed by a maintainer unconfirmed labels Mar 30, 2017
@stale
Copy link

stale bot commented Oct 17, 2017

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

@stale stale bot added the stale this has been inactive for a while... label Oct 17, 2017
@ScottFreeCode ScottFreeCode removed the type: bug a defect, confirmed by a maintainer label Oct 17, 2017
@stale stale bot removed the stale this has been inactive for a while... label Oct 17, 2017
@juergba
Copy link
Contributor

juergba commented Feb 12, 2021

closing, since outdated Mocha version and AMD modules.

@JoshuaKGoldberg
Copy link
Member

Re-filed in #5106.

@JoshuaKGoldberg
Copy link
Member

#5107 is merged; we'll release a new version with the fix soon. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants