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

Fix error not thrown #678

Merged
merged 3 commits into from
Apr 18, 2024
Merged

Fix error not thrown #678

merged 3 commits into from
Apr 18, 2024

Conversation

lojzatran
Copy link
Contributor

@lojzatran lojzatran commented Apr 13, 2024

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?

Copy link

changeset-bot bot commented Apr 13, 2024

⚠️ No Changeset found

Latest commit: a90c14b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

codecov bot commented Apr 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.83%. Comparing base (8d2df4d) to head (a90c14b).

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           
Flag Coverage Δ
integrationtests 92.83% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lojzatran lojzatran requested a review from ajimae April 13, 2024 11:45
@lojzatran lojzatran marked this pull request as ready for review April 13, 2024 11:49
@lojzatran lojzatran requested a review from a team as a code owner April 13, 2024 11:49
@ajimae
Copy link
Contributor

ajimae commented Apr 15, 2024

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?

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 .catch() block.

@lojzatran
Copy link
Contributor Author

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?

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 .catch() block.

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')
Copy link
Contributor

@ajimae ajimae Apr 15, 2024

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

@ajimae ajimae left a 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?

@lojzatran lojzatran merged commit 21e04a6 into master Apr 18, 2024
5 checks passed
@lojzatran lojzatran deleted the 647_ts-client_not_throwing_error branch April 18, 2024 13:58
@ajimae
Copy link
Contributor

ajimae commented Apr 18, 2024

⚠️ No Changeset found

Latest commit: a90c14b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets
Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

We didn't add a changeset to release this fix. I will go ahead and do that now.

@ajimae ajimae mentioned this pull request Apr 18, 2024
1 task
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.

@commercetools/ts-client 1.2.0 not throwing errors anymore
2 participants