react-native-mock icon indicating copy to clipboard operation
react-native-mock copied to clipboard

axios calls don't return

Open jasonfma opened this issue 8 years ago • 6 comments
trafficstars

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.

jasonfma avatar Apr 12 '17 22:04 jasonfma

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.

RealOrangeOne avatar Apr 13 '17 07:04 RealOrangeOne

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.

jasonfma avatar Apr 13 '17 09:04 jasonfma

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.

RealOrangeOne avatar Apr 13 '17 12:04 RealOrangeOne

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.

jasonfma avatar Apr 13 '17 14:04 jasonfma

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!

RealOrangeOne avatar Apr 14 '17 20:04 RealOrangeOne

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.

jasonfma avatar Apr 14 '17 20:04 jasonfma