How to unit test a function that uses the new Angular 14's "inject" function?

Discussão em 'Angular' iniciado por Shachar Har-Shuv, Junho 3, 2025.

  1. I've created a function that utilizes that new Angular 'inject' function. Since the inject function can be used only when initializing a class (or factory) that is part of the dependency injection tree, this function is meant to be used in the constructor of a component / service.

    I want to unit test this function with mock dependencies. The problem is I can't just call it in unit tests, because it will be called in an incorrect context. I can create a component / service just for the unit test purposes but it feels like too much boilerplate to test a simple function.

    Is there a recommended way of doing this?

    Continue reading...

Compartilhe esta Página