I'm getting familiar with JavaScript testing, and may be missing a point or two related to mocking api calls. Every tutorial I have found mocks api calls when doing unit or integration testing - example:
I dont understand the value of mocking a server response by providing hard coded data, and then testing the value of that hard coded data. It seems like all such a test does is tell you whether or not your tool used a mock instead of an actual api call. That result doesn't tell you anything about the behavior of your application though does it not? Am I missing something?
Additionally, what if I wanted to actually test the result of a real api call? Does that push me over into functional testing territory? Could a real test of an api call be done with a tool like Jest, or is that better suited for something like selenium or testcafe?
I'm getting familiar with JavaScript testing, and may be missing a point or two related to mocking api calls. Every tutorial I have found mocks api calls when doing unit or integration testing - example: https://jestjs.io/docs/en/tutorial-async
I dont understand the value of mocking a server response by providing hard coded data, and then testing the value of that hard coded data. It seems like all such a test does is tell you whether or not your tool used a mock instead of an actual api call. That result doesn't tell you anything about the behavior of your application though does it not? Am I missing something?
Additionally, what if I wanted to actually test the result of a real api call? Does that push me over into functional testing territory? Could a real test of an api call be done with a tool like Jest, or is that better suited for something like selenium or testcafe?
Share Improve this question edited Feb 8, 2019 at 9:41 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Feb 6, 2019 at 19:34 Winston SmithWinston Smith 13311 bronze badges 4- What if your testing environment backend is down? Would you want all of your tests to fail because the api returns 500 errors? No, you want to ensure that the frontend responds correctly to an api response. Similarly, imagine having thousands of tests that all require network responses. It would take so long to run. – Wolfie Commented Feb 6, 2019 at 19:38
-
3
Test isolation: wiki.c2./?UnitTestIsolation If you're doing E2E testing, then you want to hit the API. If you're unit/integration testing ponents, then you want to
reduce the scope
of your tests - test isolation. Also, you're correct, hitting the API is better done in E2E tests with something like selenium or cypress. – Adam Jenkins Commented Feb 6, 2019 at 23:33 - @Wolfie, noted, thanks. – Winston Smith Commented Feb 7, 2019 at 19:24
- @Adam, good suggestion, thank you. – Winston Smith Commented Feb 7, 2019 at 19:24
2 Answers
Reset to default 7If you want to test a call to a real api, and if that call is done by a front-end application, TestCafe offers Request Hooks that enable to check that the api is called in the right way and that the response contains the right data.
Mocking an API Response enables to do some kind of chaos testing: what happens to the front-end when API sends an HTTP 500/400/404/202/... or when the API never sends a response...
That result doesn't tell you anything about the behavior of your application though does it not
Sure it does. It tells you your front end ponent was able to correctly handle the received data (put it in a redux store, localStorage, update the UI, etc).