lmiller1990 / vue-testing-handbook

A guide on testing Vue components and applications
https://lmiller1990.github.io/vue-testing-handbook/
876 stars 160 forks source link

Write something about mocking HTTP services #170

Open Mikilll94 opened 4 years ago

Mikilll94 commented 4 years ago

I have read the whole handbook. It is great, I appreciate it 👍

However, IMO it lacks information about mocking HTTP services. Currently, I noticed that you only describe how to mock the whole axios module which is fine for demonstration purposes. However, in a real-world application, you don't want to do this. Instead, you extract your HTTP communication to separate files most-often called "services", i.e. accountService, taxService, etc. And you mock these services, not whole axios.

This is a better approach because in real-world applications you often have components with multiple HTTP calls, some logic around them, and some error handling.

And the solution like this is not fine: Screenshot 2020-09-13 at 14 18 16

because you mock all axios "GET" methods with the same response.

IMO, for mocking HTTP services, it would be nice to introduce ES6 class mocks from Jest, but this is just my proposition.

lmiller1990 commented 4 years ago

Yep, I would like to add this for sure. I think we need two sections:

  1. add an entire section about jest mocks
  2. add something small about mocking axios (or services, or whatever) on the page that talks about mocking axios. It could link to (1)

There is actually a placeholder for this (last page of the handbook, about Jest mocks).

Are either of these something you are interested in contributing?