Testing Signals with Angular Testing Library

profile
Tim Deschryver
timdeschryver.dev

If you're an Angular developer then there's a big chance that the recent new Signal APIs have caught your attention, and you probably want to use them within your project.

If that's the case and if you're wondering how these changes affect your (new and old) tests, then you're in the right place. The thing is that if you're already using Angular Testing Library, you're in luck because it's already prepared to handle these new changes.

The philosophy behind Angular Testing Library is to test your components the way your users experience them, and that's the reason why you probably won't need to change anything in your tests to make them work with the new Signal properties. Using Signals is just a technical implementation detail that doesn't affect the way your users use the application and thus they also shouldn't impact how your components are tested.

Let's take a look at some simple examples.

Testing input properties link

Just as with the "traditional" @Input() properties, you can test Signal input properties by setting the input properties in the componentInputs object when calling the render function. You will have Intellisense to autocomplete the property names, but you will be able to pass invalid property values (this can be improved in the future). While setting the input use the inner value of the Signal, not the Signal itself.

Tip

If you're using Jest, you need to upgrade Jest to v29.5 (or higher) and jest-preset-angular v14.0.1 (or higher) to be able to set the Signal properties within the test setups.

For input properties (and output) that have an alias, you can use the alias to set the input property. You won't have IntelliSense for the property name, but you can still set the property using the alias. This is also how inputs are set when using the @Input() decorator.

Testing output properties link

To test the Signal output properties, you can use the componentOutputs object when calling the render function. I prefer to use a spy that is assigned to the emit function of the output property.

Testing model properties link

To test model properties, you can use the componentInputs object when calling the render function. You can set the model property to a value, and then test that the component renders the expected output.

Access the Signal properties link

If you need to access the Signal properties directly, you can use the fixture object that is returned from the render function. This isn't recommended, but can be useful for some edge cases.

Conclusion link

When you test your components the way your users interact with them, you will make your tests more robust and less brittle. Angular Testing Library is already prepared to handle the Signal APIs, because they encourage you to test your components the way your users experience them.

Because of this you won't need to change your tests to make them work with the Signal properties. Using Signals is just a technical implementation detail that doesn't affect the way your users use the application and thus they also shouldn't impact how your components are tested.

Feel free to update this blog post on GitHub, thanks in advance!

Join My Newsletter (WIP)

Join my weekly newsletter to receive my latest blog posts and bits, directly in your inbox.

Support me

I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.

Buy Me a Coffee at ko-fi.com PayPal logo

Share this post

Twitter LinkedIn