Angular Testing module: Wrap Mock Test

Liron Hazan
ITNEXT
Published in
1 min readNov 14, 2018

--

I find Angular’s module testing pattern to be the most efficient and maintainable so I’m applying it on every Angular project I’m heading,

Keeping the following concepts really made my life easy:

  1. I’m not using web apis directly, wrapping it in a service, and use the wrapper :)
  2. I mock every service! (of my application and external api used by my services, components)

3. I have an AppTestingModule in which I import all testing-modules, and actual modules used by tested components, and I provide the stub services instead of the “real” once.

Wrap:

Let’s say, from any reason you choose to use localstorage for saving UI element position (when drag a widget for e.g.) or poc JWT authentication, Angular wrapper service for localStorage:

Mock:

Test:

That’s all :)

--

--