8000 Allow throws / throwsAsync to work with any value, not just errors · Issue #2517 · avajs/ava · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Allow throws / throwsAsync to work with any value, not just errors #2517
Closed
@novemberborn

Description

@novemberborn

t.throws() and t.throwsAsync() require the resulting exception to be a proper error:

ava/lib/assert.js

Lines 147 to 154 in 952a017

if (!isError(actual)) {
throw new AssertionError({
assertion,
message,
savedError,
values: [formatWithLabel(`${prefix} exception that is not an error:`, actual)]
});
}

I propose we add an any: boolean option to the expectations object used for these assertions. If true it won't cause a failed assertion when the exception is not an error.

We need to update the validation logic here to allow this property:

function validateExpectations(assertion, expectations, numberArgs) { // eslint-disable-line complexity

Don't forget to update the type definition:

ava/index.d.ts

Line 11 in 952a017

export type ThrowsExpectation = {

If any is true, we should try and change the typing of the return value to be unknown:

ava/index.d.ts

Line 252 in 952a017

<ThrownError extends Error>(fn: () => any, expectations?: ThrowsExpectation | null, message?: string): ThrownError;

ava/index.d.ts

Line 263 in 952a017

<ThrownError extends Error>(fn: () => PromiseLike<any>, expectations?: null, message?: string): Promise<ThrownError>;

And update our own tests:

test('.throws()', gather(t => {

test('.throwsAsync()', gather(t => {

See also the discussion in #1841.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0