Skip to content

assertThrows and assertRejects: check properties of the thrown error other than message #6592

Open
@lionel-rowe

Description

@lionel-rowe

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

For example, DOMExceptions with standardized name prop (but no standardized message prop):

// callback form `(err: E) => boolean`
assertRejects(() => new Promise((_, rej) => {
    const s = AbortSignal.timeout(0)
    s.onabort = () => rej(s.reason)
}), DOMException, (x) => x.name === 'TimeoutError')

Or custom error classes that don't use message:

class CustomError extends Error {
    readonly code: number
    constructor(code: number) {
        super()
        this.code = code
    }
}

// property bag form `{ [K in keyof E]: E[K] }`
assertThrows(() => {
    throw new CustomError(-1)
}, CustomError, { code: -1 })

Describe the solution you'd like

Allow checking properties of the thrown error other than message, e.g. with an optional errorCheck parameter.

Describe alternatives you've considered

  • How does this argument fit into the current (overloaded) function signatures?
  • Which form is better — callback, property bag, both, something else?
    • Callback is significantly more customizable (e.g. arbitrary checking of nested properties, more sophisticated checking of message prop such as exact match or regex match)
    • If supporting property bag, would need to decide how to check equality of nested data, possibly requiring an added dependency on assert/equal

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions