1

In terms of efficiency and code coverege, why would you write jasmine unit tests when you can write protractor e2e tests and mock out http requests, 3rd party API's etc'?

  1. You can make the same specific assertions such as in unit test because most of the asserted objects will have UI representation.
  2. You cover much larger flows (If I have a controller that runs a directive, that runs another directive...) .
  3. It saves time. E2E tests are much faster to write.

So i see much greater value to E2E tests with mocked http requests, yet most of the developer insist on writing unit tests.

What am I missing here?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jvrnt
  • 635
  • 1
  • 6
  • 20

1 Answers1

2

Some not properly organized and structured thoughts..

Both unit and e2e tests are needed. They have different purposes and work in a different scope.

Here is a quite interesting article on the subject of the true value of tests in e2e vs unit testing perspective: Just Say No to More End-to-End Tests which points out that, ideally, you should aim to the following pyramid, focusing primarily on unit and integration tests:

enter image description here

Unit tests are lightweight, designed to check specific functions/components of your system in isolation - they are supposed to be quick and give you an instant and very specific feedback - on what line in your code an error happens, what went wrong.

End-to-end tests on the other hand work on a much higher level - checking your application as a whole, from a user's perspective, with all of the moving parts work together. It's quite common when an e2e test failure does not give you a lot of information on what went wrong, where and what caused a failure.

Usually, unit tests are written by developers themselves, while end-to-end tests are written by testers/QA specialists.

And, there is also a lot of information on the subject out there:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Perhaps my question was not clear enough. I do understand the difference between unit and integration tests and the importance of separating them in most of the cases. But i'm talking specifically about SPA with jasmine and protractor. My point is that e2e tests with mocked out server API can tell me everything that a unit test can tell (since most of the assertions objects represented in the UI). So yes, if i have an e2e test which goes trough a broken module it won't pass, but i still will be able to know which module is broken and why. Just like in a unit test – jvrnt Nov 09 '15 at 16:25
  • @jvrnt yeah, I kind of switched focus of the answer to basic differences, sorry. Well, in a case of e2e test failure, the feedback - the error stacktrace would never be as specific and clear as in case of a unit test. You might not also be able to get a maximum test coverage and get to all of the corners of your modules with e2e tests. Plus, e2e tests would give you an unnecessary overhead of spinning up a selenium server, browser, load your complete application etc.. – alecxe Nov 09 '15 at 16:35