简体   繁体   中英

JEST - why mock api calls?

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?

If 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 component was able to correctly handle the received data (put it in a redux store, localStorage, update the UI, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM