Skip to content

🐛 Bug: process.exit(0) ends mocha silently #5672

@BridgeAR

Description

@BridgeAR

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

Mocha should patch process.exit() to throw an error in case that method is called, since it ends the whole test suite immediately.

That might be alright for non zero exit codes since the test suite would then be detected as erroring, while I believe other tests should in fact continue, similar to an uncaught exception or an unhandled rejection letting other tests continue to run.

Actual

Calling process.exit(0) in a test silently ends the process and no other test continues.

We have a big test suite and a library we are using was misconfigured. Due to that, that library called process.exit(0), which immediately ended the test suite and it swallowed other issues. This is a pretty big concern.

A mitigation is to monkey patch process.exit in a beforeAll hook and to reset that in an afterAll hook.

Minimal, Complete and Verifiable Example

'use strict'

const { describe, it, afterEach } = require('mocha')

describe('code origin', () => {
  afterEach(() => {
    console.log('afterEach')
  })

  describe('entryTags', () => {
    it('should collect expected entry tags if not given a function', () => {
      console.log('should collect expected entry tags if not given a function')
    })

    it('should collect expected entry tags if given a function', () => {
      console.log('should collect expected entry tags if given a function')
      process.exit(0)
    })

    it('should find a user frame if the stack trace limit is set to 0', () => {
      console.log('should find a user frame if the stack trace limit is set to 0')
    })
  })
})

Versions

11.7.5

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    semver-majorimplementation requires increase of "major" version number; "breaking changes"status: accepting prsMocha can use your help with this one!type: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions