Skip to content

error parameter of assert.doesNotThrow should also support types Object and Error #36281

@RaisinTen

Description

@RaisinTen

Is your feature request related to a problem? Please describe.

In this commit, I was trying to assert that the functions being tested don't throw TypeErrors.

Ref: b640874

This part throws the following error:

// eslint-disable-next-line no-restricted-syntax
assert.doesNotThrow(() => {
  fs.readSync(fd,
              Buffer.allocUnsafe(expected.length),
              0,
              expected.length,
              2n ** 63n - 1n);
}, {
  code: 'ERR_INVALID_ARG_TYPE',
  name: 'TypeError'
});
node:assert:786
    throw new ERR_INVALID_ARG_TYPE(
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "expected" argument must be of type function or an instance of RegExp. Received an instance of Object
    at new NodeError (node:internal/errors:278:15)
    at hasMatchingError (node:assert:786:11)
    at expectsNoError (node:assert:809:17)
    at Function.doesNotThrow (node:assert:834:3)
    at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-fs-read-type.js:251:8)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}
Command: out/Release/node /home/runner/work/node/node/test/parallel/test-fs-read-type.js

but the following code doesn't throw any error:

// eslint-disable-next-line no-restricted-syntax
assert.doesNotThrow(() => {
  fs.read(fd,
          Buffer.allocUnsafe(expected.length),
          0,
          expected.length,
          2n ** 63n - 1n,
          common.mustCall());
}, {
  code: 'ERR_INVALID_ARG_TYPE',
  name: 'TypeError'
});

This is a confusing because assert.throws supports Objects as an error value but assert.doesNotThrow doesn't. Also, I don't know why node isn't throwing an error for the second case given that the effective area of the code the commit is dealing with is just the same.

Describe the solution you'd like

Currently, assert.doesNotThrow only supports the following types for error:

  • RegExp
  • Function

However, assert.throws supports even more types for error:

  • RegExp
  • Function
  • Object
  • Error

It would be better to have the api to be a bit more consistent by adding support for all the types supported by assert.throws in assert.doesNotThrow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions