e2e tests vs contract tests

In order to discuss this I need to define my terms as e2e tests have many meanings. Here I base them upon outside in tests of the system that you support, tested mostly. through the user interface or public api.

For contract tests I mean systems like Pact which ensure that for a given set of api calls a certain response is given.

The system that I mainly work on has a react UI served by graphql endpoints. The e2e tests use Playwright to automate the UI and perform checks. The frontend code calls the real backend code. I have had to mock out some of the external dependencies (we can’t have an e2e test making credit card payments).

The frontend code has unit tests. The backend code has unit tests. The e2e tests ensures that one can talk to the other in a useful manner.

Contract tests can prove that a given contract will still return what is expected where as an e2e test can also check. that the call is actually being made.

e2e tests do have downsides.:

  • Slow
  • Unstable
  • Non specific errors

The main part of the slow is the setting up of the environment to run the tests. Only test enough to prove that the services are connected. Never loop through all inputs in an e2e test as this will be slow and will enhance any statistical failures.

Unstable is that these will break if the UI flow changes. They can also break if any of the external mocks change.

There are many ways for a test to fail so you don’t always get the cause shown.

The only way to overcome all of these is to have a way to run the same setup locally.
e2e tests also force you to understand all of the dependencies that you are using. Keep the boundaries as tight as you can. Sometimes this can be painful if you are consuming details from many services.