Back to the homepage
Spectator

Spectator – when testing becomes a pleasure

Angular is a nice framework, isn’t it? ? But like every tool, it has its weaknesses. A definite shortcoming is, for example, writing integration tests for templates and component classes. The TestBed testing module provided is simply unpleasant.  Can we safely say that such basic things as:

  • mocking up the component dependencies
  • access to a rich set of assertions (only basic ones from Jasmine)
  • catching elements (fixture.debugElement, brrr!!)
  • simple test cases, e.g. if “is button disabled”

Are they simple and clear? For me… definitely not! And at this point, it comes in all white:

spectator

The spectator is a pretty thoughtful abstraction on the built-in angular testing module. The main author is Netanel Basal, who also runs an angular blog – https://netbasal.com, highly recommended! The library makes test writing, readability, and maintenance significantly easier.

The main advantages of the library:

  • support for testing pipes, components, directives, HTTP services and routing
  • simplified catching of DOM tree elements (like in jQuery?)
  • richer API for running events (Mouse helpers and Keyboard helpers)
  • comfortable testing with HostComponent
  • support for ng-content testing
  • more matchers, such as .toBeDisabled() or .toBeFocused()
  • support for entry components and component providers
  • service auto-location, wow!
  • support for IS
  • global queries:

Let’s check it!

Let’s compare what the test elements in Spectator might look like compared to the TestBed Module, with simple examples.

  1. Disabled button status:

2. Does *ngFor display 2 components in a list:

3. Locking the service as a component dependency:

4. Calling “enter” on input:

5. Whether the element contains the text:

There is no denying that it looks much simpler and clearer in Spectator! You will find dozens of examples in the documentation: https://github.com/ngneat/spectator.

Installation

If I’ve convinced you of Spectator, there’s nothing left to do but add it to your project along with ng-mocks via npm:

Schematics

It is also worth mentioning that Spectator has its Schematics. For example, to generate a test file for a component, we can use the command:

Bonus!

If you code using Visual Studio Code, be sure to add the snippet plugin:

Practice with a playground from the authors:

PLAYGROUND

Enjoy testing! ?

About the author

Tomasz Nastały

Tomasz is a JavaScript Developer, Angular and TypeScript frameworks enthusiast. On a daily basis, he likes sharing his knowledge by conducting classes at one of Tricity’s boot camps and by recording Angular courses.

Don’t miss anything! Subscribe to our newsletter. Stay up-to-date with the latest trends, tips, meetups, courses and be a part of a thriving community. The job market appreciates community members.

Leave a Reply

Your email address will not be published. Required fields are marked *