Skip to content

toThrowError() Does Not Support Custom Error Types #4559

@kmanev073

Description

@kmanev073

Describe the bug

When a custom object-type error is thrown and a string parameter is passed to toThrowError() an unhanded exception is thrown:

TypeError: Cannot read properties of undefined (reading 'indexOf')
 ❯ Object.compatibleMessage node_modules/check-error/index.js:83:29

I guess vitest expects to always receive an error with a message property that has a indexOf function. Well... this seems that this is not always the case.

This happens with normal and async functions as well.

I found the issue while I was trying to test SvelteKit application where I use the error() (link) function which throws a custom HttpError (link).

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-bdugz4?file=test%2Fbasic.test.ts

I've seen this issue issue 1636 but it doesn't explain on how to assert the contents of the object instance that was thrown.

System Info

System:
    OS: Windows 11 10.0.22635
    CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
    Memory: 16.11 GB / 31.76 GB
  Binaries:
    Node: 18.18.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (120.0.2210.7)
    Internet Explorer: 11.0.22621.1

Used Package Manager

npm

Validations

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