RealOrangeOne / react-native-mock

A fully mocked and test-friendly version of react native (maintainers wanted)
MIT License
570 stars 153 forks source link

axios calls don't return #131

Open jasonfma opened 7 years ago

jasonfma commented 7 years ago

I have a suite of integration tests that use axios - which in turn uses the newly mocked out XMLHttpRequest. These calls don't return anymore.

I do stub out the API requests for the rest of my test suite, but I'd still like to be able to run integration tests to verify the contracts for some of the APIs - verifying the integration doesn't break.

Is there a way to replace the mock or unmock it? This used to work before 0.3.0.

RealOrangeOne commented 7 years ago

This is likely because the mock for XMLHttpRequest never returns anything, which is fine. Generally you shouldnt actually need to mock things this far down, and should never actually call the network.

In this case, the best solution would be to use libraries like Mockery and Sinon to mock out axios, so you dont need to touch XMLHttpRequest at all.

Alternatively, a better solution would be writing an inteface for XMLHttpRequest which allows you to stub out individual urls so it can actually resolve, in the same way Superagent-mock does for superagent.

jasonfma commented 7 years ago

Yep right now the majority of the tests are using a mock for axios so we don't actually make the network calls.

The issue I want to solve is how to still allow for a subset of integration tests to verify that those contracts haven't been broken either by changes in the client or the API code.

RealOrangeOne commented 7 years ago

I fear then you can't use react-native-mock for integration testing, as we mock out the network-calling parts of the app, so you couldn't do any real API calls with it.

jasonfma commented 7 years ago

That's unfortunate. It was working before 3.0.0. Just curious, what was the main driver for adding the mocks for the networking calls?

Maybe for now I can separate out the integration tests into a separate test run.

RealOrangeOne commented 7 years ago

The driver was someone submitted a PR (#119). I've never done integration tests, so i'd consider network requests unnecessary for react-native-mock, but clearly there's a market for it. I believe these mocks also normalize the API between version of node and react-native, so this was still the right thing to do.

I think one of the nicest and simplest solution would be adding an environment variable (eg RNMOCK_ENABLE_NETWORKING) that wouldnt mock out network stuff, so people that need network can still have it when needed. This should be really simple to do, and not be a breaking change!

jasonfma commented 7 years ago

Yeah, even better would be a way to enable/disable in the code so we can turn it on or off for specific tests. I went down the route of separating out the test suites but now I need to combine the coverage reports so it would remove that need if I could do it within a single test run.