0

I often struggle to see the benefit of unit testing, especially in a web app environment that uses a JS frontend and an API + database backend.

On both the front and back end, you would typically mock all the network requests and responses. For example on the front end we might click a button that is used to create a new widget which sends the request to the back end which creates the widget and returns a response containing the new widget.

In the above scenario we would create our initial unit tests by mocking the actual network requests and just specifying the data that we want sent / received. Everything passes,…great.

Then we change something on the backend and will change the backend unit tests accordingly. Once again all our unit tests pass. But the problem is that now our front end tests continue passing even though the back end has changed.

This continually makes me question the value of unit testing and reminds me of this:

enter image description here

How should integration testing be done with decoupled front / back ends? What tools and techniques are advised?

If you want specifics,...I generally work with Python and Django back ends coupled with React front ends.

2 Answers 2

2

What you are looking for is Contract Testing.

It asks "Do I provide the responses that components that depend on me expect?"

Situation: You have two web apps that fetch data from one service. The three have unit/component tests with proper mocking.

The web apps tests may log the mocked responses for each request. If they provide this log to the service, it can verify if it would return the same response as the mock did.

If one web app request change on the service that would break the other web app, the Contract Tests would indicate exactly which web app would be affected.

A tool such as Pact would enable you to do it.

OBS: End-to-end tests, with things such as Selenium, are best use to verify deployment configuration, to ensure the deployment correctly integrates all parts in a production environment. Due its slowlyness and weak precision, they should be avoided as much as possible.

1
  • 1
    This is just the kind of thing that I was looking for. Thanks.
    – darkpool
    Commented Sep 24, 2018 at 14:49
0

You should add integration tests which test multiple components of your application such as the integration between frontend and backend. Also, end to end tests which test the user's journey through your application. Unit tests aren't designed to test an integrated system.

2
  • I was hoping for more specifics as per my original question. The problem is, how exactly do you do integration or end to end tests where the backend is an api that is separate from a javascript based front end. They are completely different code bases using different frameworks.
    – darkpool
    Commented Sep 23, 2018 at 4:52
  • Use a testing framework like Selenium to drive your frontend. Use a log dump of your backend to check for any errors in the log files as you automate the frontend. Parse the logs for errors that occur. See if they correlate with your frontend automation. Most frontend frameworks have features that integrate with Selenium. Integration can be several components or the entire system. You can do backend rest to add items to your database and then do a sql query to see if those items exist.
    – newsn31
    Commented Sep 23, 2018 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.