-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
base: master
Are you sure you want to change the base?
Conversation
@@ -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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.unreachable('Function threw correct pattern when it should have thrown incorrect one'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
🤔
There was a problem hiding this comment.
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.
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>
is there anything i can help with to make this land? would love to use it in tsconfck tests. |
Not that I recall 👍 kept this open for the next feature release |
Is there something similar to a |
Friendly bump 😅 |
This implements the async equivalent of
assert.throws()
for promise rejections (and releated tests, based on the tests forthrows
).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 waythrows
is always sync andrejects
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