Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Use Polly to mock HTTP requests in tests
**tl;dr:** I've introduced [Polly] as a replacement for Nock as I believe it will lead to more readable and authentic tests. A few things: * This is a proof of concept – I've only added it to CollectiblesController to start out with. * All I've done is replace Nock requests with Polly requests. Ideally, we wouldn't need to mock anything manually at all and let Polly do its thing. Unfortunately, some HTTP requests that we are mocking no longer work. * In the future, we should investigate using Ganache instead of hitting mainnet and exposing our Infura project id in tests. [Polly]: https://netflix.github.io/pollyjs/#/README --- Currently there are a couple of problems with our tests as it relates to network requests: 1. We do not prevent network requests from being made. There are some tests which use Nock to mock requests, but not all requests are being mocked, and we are unaware which ones are actually going through. 2. Nock, although a popular package, is rather cumbersome to use, and makes tests difficult to understand and follow. To address these problems, this commit replaces Nock with Polly. Polly is a package developed by Netflix that intercepts all HTTP requests and automatically captures snapshots (recordings) of these requests as they are being made. These recordings are then used in successive test runs. You can also manually mock requests if you want more manual control. When mocking requests with Nock, you must first hit the request you want to mock for real and obtain its response data. From here you can either copy the data and paste it straight into your test, or you can provide mock data based on the shape of the data. However, there are two problems with this: 1. Mocks take up space in your test, so you will probably find yourself extending a collection of mocks you've assembled at the top of the file. Once you have enough, who knows which mocks serve which tests? 2. Once you've added a mock to your test, that mock stays forever. If the API changes in the future, or stops working altogther, you will never know until you remove that mock. Polly removes both of these problems: 1. Because Polly records request snapshots, you don't usually have to worry about mocking requests manually. These recordings are kept in files which are categorized by the tests which made the requests. 2. Because Polly actually hits the API you're mocking, you can work with real data. You can instruct Polly to expire these recordings after a designated timeframe to guarantee that your code doesn't break if the API ceases to work in the way you expect. It's true that Nock contains a feature to capture requests called [Nock Back]. However, this is cumbersome, too: [Nock Back]: https://github.com/nock/nock#nock-back 1. You have to wrap your test in a `nockBack` call. 2. You have to name your recording files. 3. You have to call `nockDone()` in your test. There is a package called `jest-nock-back` that fixes these issues and makes using Nock Back very easy. However, it isn't maintained (last release was 3 years ago) and there isn't a way to automatically expire your recordings (which I believe is a key feature). Some requests make irreversible changes to resources, such as transferring a token from one account to another. To handle these types of requests, Polly still lets you manually mock requests just like Nock. We've configured Polly to not record a request it doesn't recognize by default, so in this case you'll get a message when running the test that makes such a request. From there you can make a decision about how you'd like to mock this request — whether you want Polly to record the request or whether you'd like to manually mock it. That said, if the request in question involves a blockchain network, instead of mocking the request, we might investigate whether it's possible to use Ganache to perform the irreversible action rather than actually hitting mainnet or some other network. * Because Polly automatically creates request mocks and these mocks are no longer contained in tests, developers will need to be educated about Polly (Polly is not as popular as Nock) and remember that it exists whenever updating tests. * If a recording is no longer needed or needs to be updated, the associated file holding the recorded information about the request needs to be deleted. Finding this file is not a great experience. * Nock has virtually zero initialization code. Polly's initialization code is surprisingly complicated (although that is largely hidden, so no one should have to mess with it).
- Loading branch information