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

Mocking test data #208

Closed
wants to merge 4 commits into from
Closed

Conversation

jtfirek
Copy link
Collaborator

@jtfirek jtfirek commented Apr 8, 2023

Hey @dawsbot Is this what you are looking for to mock the functions. I have created this draft to see if I am on the right track. This is everything I thought would need to be added for account for this test:

https://github.com/jtfirek/essential-eth/blob/2a34571081d9d136f78931503c9b771d2b5436e0/src/providers/test/json-rpc-provider/get-balance.test.ts

@codecov-commenter
Copy link

codecov-commenter commented Apr 8, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (81036f5) 90.65% compared to head (2a34571) 90.65%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #208   +/-   ##
=======================================
  Coverage   90.65%   90.65%           
=======================================
  Files          39       39           
  Lines        1017     1017           
  Branches      289      289           
=======================================
  Hits          922      922           
  Misses         85       85           
  Partials       10       10           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@jtfirek jtfirek marked this pull request as draft April 8, 2023 00:45
@dawsbot
Copy link
Owner

dawsbot commented Apr 20, 2023

Wow yes! This is the exact functionality. The pattern I would prefer instead is to have the mocking done in the test file directly and never using the __mock style because that's easy to lose/confuse in the codebase.

I think the best first-PR here is to find part of the codebase with a simple network request and mock out the network portion. Perhaps getBlock is the right first-example where we do mocking of the fetch and that's all we mock. The test should then work fully offline 🙌

  1. Mock the fetch with not jest.fn() but with the actual response from what it would return online (add logging locally first to it then you can remove the logs)
  2. Do a jest validation that the mocked fn was called with the valid params

Does this seem reasonable @jtfirek ? If not I can jump in 🙌

@dawsbot
Copy link
Owner

dawsbot commented Apr 21, 2023

Actually on second glance these mocks are pretty far off because it entirely removes our ability to test the function in our codebase. We need to mock only the networking part of the test like so: https://jestjs.io/docs/mock-functions#mocking-modules

But in that example they use axios. For essential-eth we would mock isomorphic-unfetch

@dawsbot dawsbot closed this in #211 Apr 21, 2023
@jtfirek jtfirek deleted the jacob/mock-test-data branch May 8, 2023 15:36
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.

3 participants