-
Notifications
You must be signed in to change notification settings - Fork 25
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
Fix error not thrown #678
Fix error not thrown #678
Conversation
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #678 +/- ##
=======================================
Coverage 92.83% 92.83%
=======================================
Files 25 25
Lines 279 279
Branches 12 12
=======================================
Hits 259 259
Misses 20 20
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
So I tried it locally, the error is not being throw, it is being returned as a response as well, instead of being rejected and passed to the the |
Can you provide more details which error is that so I can write another test for it? I tested for 404 HTTP error and it is now throwing. |
.get() | ||
.execute() | ||
|
||
throw new Error('Should have thrown an error') |
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.
Indeed it does work for me now, however I think this line shouldn't be here, the sdk call chain should be the one throwing the error when the .execute()
is called, then we can process the error and make our checks in the .catch()
block.
So aparently, try-catch will not work here because of the async nature of the builder chain, you might want to remove the try-catch and handle the error in the .cache()
.
it('should throw error when a product type is not found', async () => {
await apiRootV3
.productTypes()
.withId({ ID: 'non-existing-id' })
.get()
.execute()
.catch(e => {
expect(e.statusCode).toEqual(404)
})
})
I tried the above and it worked just fine. So we can restructure the test a bit to include the catch block instead of throwing a generic error in the try-catch
setup.
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 for the suggestion. I checked it and there is one problem in your test: if the error is not thrown (the code before my changes), then the test would also pass. This means that it does not test my fix.
Here I explained it a bit better:
it('should throw error when a product type is not found', async () => {
await apiRootV3
.productTypes()
.withId({ ID: 'non-existing-id' })
.get()
.execute()
.then(() => {
console.log('it should throw an error when ID is non-existing and not reach this console.log')
fail() // <--- a method to fail the test
})
.catch(e => {
expect(e.statusCode).toEqual(404)
})
})
There is usually fail()
method to fail the test if it reaches a place it shouldn't reach, but it seems to be missing in jest. If you didn‘t encounter it before, it‘s ok and I will leave the test as it is.
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.
Since this is an integrated test, I want to believe that the test must fail, since the ID: 'non-existing-id' is actually a non existent ID.
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.
@lojzatran this 👆🏽 is my finding. What do you think?
We didn't add a changeset to release this fix. I will go ahead and do that now. |
Fixes: #647
@ajimae : I tried to fail the test in case the error is not thrown, but
fail()
is not supported by jest: jestjs/jest#11698. Do you know how to do this?