Skip to content

throws doesn't work correctly with promises #123

Closed
@SamVerschueren

Description

@SamVerschueren

So, I tried to use the throws method in some libraries and found out it doesn't work as it should be or as we expect it should work. Haven't found a solution for this but I have some interesting information (I guess).

Immediately rejected promises

This works correctly, if a promise is rejected immediately, the test is done correctly.

test('immediate rejection', async t => {
    await t.throws(throws(), /help me/);

    function throws() {
        return Promise.reject(new Error('hello world'));
    }
});

Test will fail because hello world !== 'help me'
Note 2: Can't pass in the string help me as second argument of throws because then the test succeeds. But that is another issue.

Long running promises

Think about network requests, etc. This does not work, the throws assertion does not wait untill the promise is rejected or resolved.

test('test me', async t => {
    await t.throws(throws(), /help me/);

    function throws() {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                reject(new Error('hello world'));
            }, 2000);
        });
    }
});

This test succeeds.

An interesting thing I noticed is that the test succeeds immediately, it doesn't wait 2 seconds untill the promise is rejected.

Any thoughts?

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