-
-
Notifications
You must be signed in to change notification settings - Fork 957
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
Make ParseError checking more clear #273
Make ParseError checking more clear #273
Conversation
Why not pass those dummy values to ParseError in test? |
That would be much nicer @floatdrop! I didn't do this straight off because I didn't know it was possible. Looking at the ava docs for
Regardless, the following works: const err = await t.throws(got(`${s.url}/non200-invalid`, {json: true}),
got.ParseError(new Error(), 500, {}, '')); But to my understanding that's passing an instance, not any of the above, and to make things worse, once I comply with the linting rules and use the 'new' keyword things stop working and I get confused. Started digging around in the node source, and played around with argument binding, but neither have worked out yet and sadly I don't have more time this morning. I'll try and continue this soon! Thanks for the suggestion @floatdrop. |
Maybe this would work, or if not, we could always just check the individual properties: const err = await t.throws(got(`${s.url}/non200-invalid`, {json: true}));
t.deepEqual(err, got.ParseError(new Error(), 500, {}, '')); |
I figured out why it worked without the Now that I understand some of the internals of Errors are notoriously inaccessible, you can't iterate them, I tried anyway, but deepEqual is out. I really like @sindresorhus 's suggestion to just check props then, the way I see it that means we have three okay options. Every error should have a constructor, a message, and a name. That would respectively make the output when some non ParseError was thrown: I like constructor best because I know it's a solid way to tell if two instances come from the same object. Visually, using constructor sucks and comparing name seems most clear to me. Sorry for taking so long to get back to you two. I'd say it's your call! |
621c311
to
22b76aa
Compare
test/json.js
Outdated
@@ -74,7 +74,8 @@ test('catches errors on invalid non-200 responses', async t => { | |||
}); | |||
|
|||
test('should have statusCode in err', async t => { | |||
const err = await t.throws(got(`${s.url}/non200-invalid`, {json: true}), got.ParseError); | |||
const err = await t.throws(got(`${s.url}/non200-invalid`, {json: true})); | |||
t.is(err.constructor, new got.ParseError(new Error(), 500, {}, '').constructor); |
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.
Why not just t.is(err.constructor, got.ParseError);
?
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.
Fair question. Too long ago to remember what the issue was. Fairly sure I tried. But maybe I wrongly assumed t.is
would run into the same problem as t.throws
. I'll try tonight!
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.
You were super right. That works fine. Updated the PR.
Since we're dealing with a custom error constructor `t.throws` has a hard time checking the instance is the one we expect. This commit changes checking to use the constructor error property instead.
22b76aa
to
8642449
Compare
👍 |
JSON test 'should have statusCode in err' requires an instance of
ParseError to compare to but has no
opts
ordata
to pass. This commitmakes those arguments optional allowing for empty ParseError
construction.