Skip to content

toThrowError() treats empty string param as though it is undefined #6618

@U-4-E-A

Description

@U-4-E-A

Describe the bug

toThrowError() treats an empty string (toThrowError('')) as it would if the value was undefined when it should reject an empty string as it is not equal to the error.

Reproduction

import { expect, test } from 'vitest'

function getFruitStock(type: string): void {
  if (type === 'pineapples') {
    throw new Error('Pineapples are not in stock')
  }
}

test('throws on pineapples', () => {
  // undefined error passes
  expect(() => getFruitStock('pineapples')).toThrowError()
  // error of empty string should fail as it does not represent the error but passes
  expect(() => getFruitStock('pineapples')).toThrowError('')
})

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i7-13700H
    Memory: 4.36 GB / 15.73 GB
  Binaries:
    Node: 22.4.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.8.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    vitest: ^2.0.5 => 2.1.1

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-edge-caseBug, but has workaround or limited in scope (priority)pr welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions