Skip to content

It would be convenient if assert.rejects() and assert.throws() returned the error value #1824

@MatmaRex

Description

@MatmaRex

It would be convenient if assert.rejects() and assert.throws() returned the error value.

When a more complex assertion is needed, I find the matcher callback to be a bit awkward, especially if the rest of the test code uses await and can mostly avoid nested callbacks. It would also make tests that check success and error cases more similar to each other.

Basically, I would like to be able to write tests like this:

QUnit.test('getResultAsync (OK)', async (assert) => {
    let result = await getResultAsync('foo');
    assert.true(result instanceof MyResult)
    assert.strictEqual(result.value, 'FOO');
});

QUnit.test('getResultAsync (error, proposed API)', async (assert) => {
    let error = await assert.rejects(getResultAsync(''));
    assert.true(error instanceof MyError)
    assert.strictEqual(error.errorCode, 'empty-param');
});

Here's a runnable demo with some more examples, and the alternatives possible using the currently existing API.
test.html

This could be implemented by just adding return actual; at the end of the throws and rejects functions in https://github.com/qunitjs/qunit/blob/main/src/core/assert.js.

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