Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert.rejects and assert.not.rejects methods for promises #132

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

aral
Copy link

@aral aral commented Aug 10, 2021

This implements the async equivalent of assert.throws() for promise rejections (and releated tests, based on the tests for throws).

While this can be handled via a workaround, having a separate method does cut down on boilerplate.

Instead of overloading throws, I feel it makes sense to have a separate method for promise rejections. This way throws is always sync and rejects is always async.

I understand if you’d rather just keep throws and the workaround, just wanted to share my addition upstream. Please feel free to close this pull request and its corresponding issue if you feel this is out of scope or complicates the API beyond what you’re comfortable with.


Closes #131

src/assert.js Outdated Show resolved Hide resolved
src/assert.js Outdated Show resolved Hide resolved
src/assert.js Outdated Show resolved Hide resolved
src/assert.js Outdated Show resolved Hide resolved
@@ -366,6 +366,7 @@ throws('should be a function', () => {
throws('should throw if function does not throw Error :: generic', () => {
try {
$.throws(() => 123);
assert.unreachable('Function threw when it shouldn’t have');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.unreachable('Function threw when it shouldn’t have');

@@ -375,6 +376,7 @@ throws('should throw if function does not throw Error :: generic', () => {
throws('should throw if function does not throw matching Error :: RegExp', () => {
try {
$.throws(() => { throw new Error('hello') }, /world/);
assert.unreachable('Function threw correct pattern when it should have thrown incorrect one');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.unreachable('Function threw correct pattern when it should have thrown incorrect one');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in because I was confused when I changed the test to $.throws(() => { throw new Error('world') }, /world/); and it still passed. Wouldn’t the same thing happen if there was a regression in throws itself that would go unnoticed without this check?

Copy link
Author

@aral aral Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think this is – and the one above; same issue – are the only two I’m confused on. Accepted all other changes and happy to go with what you think is best for on these.)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion helper is saying: given X function, an error (a) should be thrown and (b) the thrown error should match {this condition}. The condition can be defined via a function, a RegExp, or a string.

So this test has X function, which happens to throw new Error('hello'), and the assertion is saying X must throw an Error that matches the /world/ pattern.

In the original test, this fails (obv because "hello" does not match /world) and so the assertions within the catch block run, and correctly find that the "Expected function to throw exception matching /world/ pattern" message appears, among others.

When you change the X to be:

$.throws(() => { throw new Error('world') }, /world/);

This doesnt fail the assertion, because "world" does match the /world pattern. The assertion passed. So in this case, the checks within the catch block never run.

Testing negated fail conditions can be a trip, haha

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda feel like it could be helpful to make that work via

$.throws(() => throw new Error('world') }).matches(new Error('world'));

which maybe that doesn't make sense.. but it'd be nice to be able to chain throws() with match() rather than having a regex over err.message 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks but the current API is much preferred and has prior art from many existing test runners and/or assertion libraries. It would also add a bunch of additional changes for the same (but more verbose) result.

test/assert.js Show resolved Hide resolved
aral and others added 5 commits August 11, 2021 14:26
Remove error type fix for now

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
Remove error type fix for now

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
Improve phrasing

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
Improve phrasing

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
@dominikg
Copy link

is there anything i can help with to make this land? would love to use it in tsconfck tests.

@lukeed
Copy link
Owner

lukeed commented Oct 10, 2021

Not that I recall 👍 kept this open for the next feature release

@bleucitron
Copy link

Is there something similar to a .resolves() that would be an alias to .not.rejects() ?

@lukeed lukeed mentioned this pull request Nov 4, 2021
@maraisr
Copy link
Sponsor Contributor

maraisr commented Nov 23, 2021

Friendly bump 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

assert.rejects() and asserts.not.rejects() for promises
6 participants