Bug: Angular 14+ using the inject with defaultMock is not working #5073
-
Description of the bugWhen using MockBuilder, i try to inject a custom InjectionToken with the new "inject" function from angular 14, and trying to mock it globally with a "ngMocks.defaultMock" the test fail with a "No provider for InjectionToken" error. It's working when using the "old way" of doing injection with the constructor and the @Inject decorator or if i provide a mock with the MockBuilder.mock function. An example of the bugLink: https://github.com/KevinBonnoron/ng-mocks-inject-bug Expected vs actual behaviorExpected behaviour: The test should pass Actual behaviour: The test is failing The global mock for the token is done in src/setupJest.ts I commented some code and added comments to indicate what parts are working and what aren't. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi there, you are very close. Because beforeAll(() => MockBuilder(AppComponent, [ENVIRONMENT_TOKEN])); Otherwise The correct version of your test: https://codesandbox.io/s/silly-wescoff-xkg4ri?file=/src/test.spec.ts:641-658 |
Beta Was this translation helpful? Give feedback.
Hi there,
you are very close.
Because
ENVIRONMENT_TOKEN
is a dependency, it should be provided inMockBuilder
:Otherwise
ng-mocks
cannot detect it, because ofinject
it's a runtime dependency, whereas before with@Inject
it was a declaration dependency, which could be detected without executing code.The correct version of your test: https://codesandbox.io/s/silly-wescoff-xkg4ri?file=/src/test.spec.ts:641-658