Skip to content

🐛 Bug: Error from afterEach hides the original error from the test case when retries > 0 #5007

@zorji

Description

@zorji

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) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Error from afterEach hides the original error from the test case when retries is used (> 0).

Steps to Reproduce

Here is the minimum test case to reproduce the issue

describe('test after each error', () => {

  afterEach(async () => {
    throw new Error('Error from after each');
  });

  it('test one', async () => {
    throw new Error('Error from test');
  });

});

Then run the following to see the difference

npx mocha --retries 0 index.spec.js
npx mocha --retries 1 index.spec.js

Expected behavior: [What you expect to happen]

The result should tell me the error raised from the test case

npx mocha --retries 0 index.spec.js


  test after each error
    1) test one
    2) "after each" hook for "test one"


  0 passing (3ms)
  2 failing

  1) test after each error
       test one:
     Error: Error from test
      at Context.<anonymous> (index.spec.js:8:11)
      at process.processImmediate (node:internal/timers:476:21)

  2) test after each error
       "after each" hook for "test one":
     Error: Error from after each
      at Context.<anonymous> (index.spec.js:4:11)
      at process.processImmediate (node:internal/timers:476:21)

Actual behavior: [What actually happens]

The output did not show the error raised in the test case but only show the error from the clean up afterEach

npx mocha --retries 1 index.spec.js


  test after each error
    1) "after each" hook for "test one"


  0 passing (2ms)
  1 failing

  1) test after each error
       "after each" hook for "test one":
     Error: Error from after each
      at Context.<anonymous> (index.spec.js:4:11)
      at process.processImmediate (node:internal/timers:476:21)

Reproduces how often: [What percentage of the time does it reproduce?] 100%

Versions

  • The output of mocha --version and node_modules/.bin/mocha --version: 10.2.0 for both
  • The output of node --version: v18.16.1
  • Your operating system
    • name and version: OSX 13.5.1
    • architecture (32 or 64-bit): 64
  • Your shell (e.g., bash, zsh, PowerShell, cmd): iTerm
  • Your browser and version (if running browser tests):
  • Any third-party Mocha-related modules (and their versions):
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): No

Additional Information

Nothing

Metadata

Metadata

Assignees

Labels

status: accepting prsMocha can use your help with this one!type: buga defect, confirmed by a maintainer

Type

No type
No fields configured for issues without a type.

Projects

Status
No status

Relationships

None yet

Development

No branches or pull requests

Issue actions