Repository: nodejs/node
File: test/parallel/test-util-inspect.js (JavaScript)

Code before:
    '        at get bar (/foo/node_modules/bar.js:827:30))>]\n' +
    '  })>]\n' +
    '}'
  );
}

// Property getter throwing an error with getters that throws recursivly.
{
  const recursivelyThrowingErrorDesc = {
    __proto__: null,
    // eslint-disable-next-line no-restricted-syntax
    get() { throw createRecursivelyThrowingError(); },
    configurable: true,
    enumerable: true,
  };
  const createRecursivelyThrowingError = () =>
    Object.defineProperties(new Error(), {
      cause: recursivelyThrowingErrorDesc,
      name: recursivelyThrowingErrorDesc,
      message: recursivelyThrowingErrorDesc,
      stack: recursivelyThrowingErrorDesc,
    });
  const thrower = Object.defineProperty({}, 'foo', recursivelyThrowingErrorDesc);

  assert.strictEqual(
    inspect(thrower, { getters: true, depth: 2 }),
    '{\n' +
    '  foo: [Getter: <Inspection threw ([object Error] {\n' +
    '    stack: [Getter/Setter: <Inspection threw ([object Error] {\n' +
    '      stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +
    '      cause: [Getter: <Inspection threw ([Error])>],\n' +
    '      name: [Getter: <Inspection threw ([Error])>],\n' +
    '      message: [Getter: <Inspection threw ([Error])>]\n' +
    '    })>],\n' +
    '    cause: [Getter: <Inspection threw ([object Error] {\n' +
    '      stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +
    '      cause: [Getter: <Inspection threw ([Error])>],\n' +
    '      name: [Getter: <Inspection threw ([Error])>],\n' +
    '      message: [Getter: <Inspection threw ([Error])>]\n' +
    '    })>],\n' +
    '    name: [Getter: <Inspection threw ([object Error] {\n' +
    '      stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +
    '      cause: [Getter: <Inspection threw ([Error])>],\n' +
    '      name: [Getter: <Inspection threw ([Error])>],\n' +
    '      message: [Getter: <Inspection threw ([Error])>]\n' +
    '    })>],\n' +
    '    message: [Getter: <Inspection threw ([object Error] {\n' +
    '      stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +
    '      cause: [Getter: <Inspection threw ([Error])>],\n' +
    '      name: [Getter: <Inspection threw ([Error])>],\n' +
    '      message: [Getter: <Inspection threw ([Error])>]\n' +

Review comment (suggestion):
We should probably verify that not setting the value produces the same result
```suggestion
    inspect(thrower, { getters: true }),
```
And I would add another test case for testing a non-default value (e.g. `depth: 1`)

Code after:
    '    cause: [Getter: <Inspection threw (Error: Oops\n' +
    '        at get foo (/foo/node_modules/foo.js:2:7)\n' +
    '        at get bar (/foo/node_modules/bar.js:827:30))>]\n' +
    '  })>]\n' +
    '}'
  );
}

// Property getter throwing an error with getters that throws recursivly.
{
  const recursivelyThrowingErrorDesc = {
    __proto__: null,
    // eslint-disable-next-line no-restricted-syntax
    get() { throw createRecursivelyThrowingError(); },
    configurable: true,
    enumerable: true,
  };
  const createRecursivelyThrowingError = () =>
    Object.defineProperties(new Error(), {
      cause: recursivelyThrowingErrorDesc,
      name: recursivelyThrowingErrorDesc,
      message: recursivelyThrowingErrorDesc,
      stack: recursivelyThrowingErrorDesc,
    });
  const thrower = Object.defineProperty({}, 'foo', recursivelyThrowingErrorDesc);

  assert.strictEqual(
    inspect(thrower, { getters: true, depth: 1 }),
    '{\n' +
    '  foo: [Getter: <Inspection threw ([object Error] {\n' +
    '    stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +
    '    cause: [Getter: <Inspection threw ([Error])>],\n' +
    '    name: [Getter: <Inspection threw ([Error])>],\n' +
    '    message: [Getter: <Inspection threw ([Error])>]\n' +
    '  })>]\n' +
    '}'
  );

  [{ getters: true, depth: 2 }, { getters: true }].forEach((options) => {
    assert.strictEqual(
      inspect(thrower, options),
      '{\n' +
      '  foo: [Getter: <Inspection threw ([object Error] {\n' +
      '    stack: [Getter/Setter: <Inspection threw ([object Error] {\n' +
      '      stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +
      '      cause: [Getter: <Inspection threw ([Error])>],\n' +
      '      name: [Getter: <Inspection threw ([Error])>],\n' +
      '      message: [Getter: <Inspection threw ([Error])>]\n' +
      '    })>],\n' +
      '    cause: [Getter: <Inspection threw ([object Error] {\n' +
      '      stack: [Getter/Setter: <Inspection threw ([Error])>],\n' +