Skip to content

🐛 Bug: Parallel mode crashes if test exception contains circular references #4552

@medikoo

Description

@medikoo

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

Description

Parallel mode crashes or hangs if test exception contains circular references

Steps to Reproduce

1. Hangs if error has self reference:

Test file test.js

describe('Test', () => {
  it('test', () => {
    const error = new Error('Foo');
    error.self = error;
    throw error;
  });
});

Running following will simply hang:

$ mocha --parallel test.js

2. Crashes if error has circular references passed within array properties

Test file test.js

describe('Test', () => {
  it('test', () => {
    const error = new Error('Foo');
    error.foo = { props: [] };
    error.foo.props.push(error.foo);
    throw error;
  });
});

Running following

$ mocha --parallel test.js

will result with an error as:

 1) Uncaught error outside test suite

  0 passing (308ms)
  1 failing

  1) Uncaught error outside test suite:
     Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'props' -> object with constructor 'Array'
    --- index 0 closes the circle
      at stringify (<anonymous>)
      at writeChannelMessage (node:internal/child_process/serialization:120:20)
      at process.target._send (node:internal/child_process:819:17)
      at process.target.send (node:internal/child_process:719:19)
      at processTicksAndRejections (node:internal/process/task_queues:93:5)

Expected behavior:

Originally thrown exception should surface in report

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 8.2.1
  • The output of node --version: 15.5.1
  • Your operating system
    • name and version: macOS 10.15.7
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): zsh

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions