Skip to content

Enforce throwing error object with t.throws #1440

Closed
@sindresorhus

Description

@sindresorhus

Supporting throwing string (which is a very bad practise anyways) leads to problems like #1439.

import test from 'ava';

test(t => {
	t.throws(() => {
		throw 'foo';
	}, Error);
});
  1 failed

  [anonymous]
  /Users/sindresorhus/dev/private/ava-playground/test.js:4

   3: test(t => {
   4:   t.throws(() => {
   5:     throw 'foo';

  Threw unexpected exception:

  'foo'

It's not entirely clear here that the problem is that it's excepting an Error object, but a string was thrown. Instead of improving the error message for this case, we should just disallow throwing a string altogether, so when a string is thrown, we could warn like this:

Threw a string, but the assertion requires an Error object

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions