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

🐛 Bug: Tests pass without running after uncaught exception #5251

Open
4 tasks done
JoshuaKGoldberg opened this issue Nov 13, 2024 · 3 comments
Open
4 tasks done

🐛 Bug: Tests pass without running after uncaught exception #5251

JoshuaKGoldberg opened this issue Nov 13, 2024 · 3 comments
Labels
status: in triage a maintainer should (re-)triage (review) this issue type: bug a defect, confirmed by a maintainer

Comments

@JoshuaKGoldberg
Copy link
Member

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have '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, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

If an uncaught exception occurs, I would think Mocha should report a failure and continue running subsequent tests.

Actual

If a test throws an error inside a process.nextTick callback, Mocha correctly reports that test failing, but then doesn't run subsequent tests. It reports them as passed.

For example, in the MRE, there are three tests -a, b, and c- and b throws an error inside process.nextTick. The report is:

  a
    ✔ should pass

  b
    ✔ should pass, then fail
    1) should pass, then fail


  2 passing (3ms)
  1 failing

  1) b
       should pass, then fail:
     Uncaught Error: uncaught!!
      at /Users/josh/repos/repros/test/example-b.js:4:15
      at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

Minimal, Reproducible Example

https://github.com/JoshuaKGoldberg/repros/tree/mocha-skip-test-after-uncaught

git clone https://github.com/JoshuaKGoldberg/repros -b mocha-skip-test-after-uncaught
npm i
npx mocha

Versions

  • Mocha: 10.8.2
  • Node: 18, 20, and 22

Additional Info

Discovered in #5250 -> 6b5ff68.

Somewhat similar-looking to #5184 but not the same.

@JoshuaKGoldberg JoshuaKGoldberg added status: in triage a maintainer should (re-)triage (review) this issue type: bug a defect, confirmed by a maintainer labels Nov 13, 2024
@wnghdcjfe
Copy link
Contributor

Hello@JoshuaKGoldberg
I think this error can be resolved in the following two ways.
Are you talking about the part where the fail should appear when an error in process.nextTick occurs?

  1. try - catch
describe('b', function() {
  it('should pass, then fail', function(done) {
    process.nextTick(function() {
      try {
        throw new Error('uncaught!!');
      } catch (err) {
        done(err); // Mocha에 에러를 알림
      }
    });
  });
}); 

Image

  1. promise
describe('b', function() {
  it('should pass, then fail', function() {
    return new Promise((resolve, reject) => {
      process.nextTick(function() {
        reject(new Error('uncaught!!'));  
      });
    });
  });
});

Image

@JoshuaKGoldberg
Copy link
Member Author

I don't follow what you're asking, sorry. But if you can send a PR that would help me understand :)

@wnghdcjfe
Copy link
Contributor

Okay. I'll PR it later.
First of all, I found that it occur "pass" in beforeEach hook.
Image

If the code is error-handling well, it should appear as follows.
Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: in triage a maintainer should (re-)triage (review) this issue type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

2 participants