Skip to content

Allow Failing With Rejection Message On Async Tests

Compare
Choose a tag to compare
@ealush ealush released this 09 Sep 08:23
· 64 commits to master since this release

By @michasherman

Rejecting with rejection message

What if your promise can reject with different messages? No problem!
You can reject the promise with your own message by passing it to the
rejection callback.

Notice that when using rejection messages we do not need to pass statement
argument to test. This means that the statement will always be inferred
from the rejection message.

In case you do pass statement, it will serve as a fallback message in any
case that the rejection message is not provided.

test('name', new Promise((resolve, reject) => {
    fetch(`/checkUsername?name=${name}`)
        .then(res => res.json)
        .then(data => {
            if (data.status === 'fail') {
                reject(data.message); // rejects with message and marks the test as failing
            } else {
                resolve(); // completes. doesn't mark the test as failing
            }
        });
}));