Skip to content

t.throws should return the error (or a promise that resolves with the rejection) #493

Closed
@jamestalmage

Description

@jamestalmage

From #492

test(t => {
  t.plan(2);
  return shouldRejectWithFoo().catch(reason => {
    t.is(reason.message, 'HelloMessage') // Prefer t.throws if all you care about is the message
    t.is(reason.foo, 'bar');
  });
});

test(t => {
  t.plan(2);
  try {
    shouldThrow();
  } catch (err) {
    t.is(err.message, 'HelloMessage') // Prefer t.throws if all you care about is the message
    t.is(err.foo, 'bar');
  }
});

In most cases, you should prefer the t.throws() assertion, but this is an acceptable use since t.throws() only allows you to assert against the error's message property.

We could make that recommendation obsolete:

test(async t => {
  var err = await t.throws(shouldRejectWithFoo, 'HelloMessage');
  t.is(err.foo, 'bar');
});

test(t => {
  var err = t.throws(shouldThrow, 'HelloMessage');
  t.is(err.foo, 'bar');
});

See also: #468 (it also changes the behavior of t.throws).

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