question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tests fail on dependency injection after upgrading to Angular 9

See original GitHub issue

Describe the Bug

Tests fail on components and services (and probably other things I have not yet tested) which have dependencies (in the constructor) after upgrading to Angular 9, with the following error: TypeError: Cannot read property 'ɵprov' of undefined

Minimal Reproduction

  • create a project with the latest CLI version (currently 9.0.1)

  • setup the project following the guidelines from https://medium.com/angular-in-depth/angular-cli-ng-test-with-jest-in-3-minutes-v2-1060ddd7908d

  • create a service using the CLI

  • add a dependency to the service’s constructor (for instance HttpClient)

  • run ng test

  • test fails with the following error: TypeError: Cannot read property 'ɵprov' of undefined

Adding HttpClientTestingModule to TestBed imports makes no difference.

Minimal reproduction repo: https://github.com/bisonfoutu/jest-ng-9-issue

Expected Behavior

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment


Libs
- @angular/core version: 9.0.0
- @angular-devkit/build-angular version: 0.900.1
- @angular-builders/jest version: 9.0.0-beta.3

For Tooling issues:
- Node version: 12.14.0  
- Platform: Windows 

Others:

Additional Context

Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

16reactions
just-jebcommented, Feb 18, 2020

Yes, I cloned, indeed there was a problem.
You need to set "emitDecoratorMetadata": true in compilerOptions of your tsconfig.spec.json. Once you do this it starts working. More details here. I’ll update the article.

2reactions
michaelfaithcommented, Oct 24, 2020

I’m seeing this error as well, on tests for anything that has dependencies. And i do have emitDecoratorMetadata set to true. I didn’t at first, but then found this issue, and added it. But, it still has the same behavior.

I just realized this happens on any Angular element having dependencies, I updated the title and bug description accordingly.

The test still does not pass, but it returns a different error:

This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.```
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jasmine tests in Angular 9 - dependency injection errors
Jasmine tests in Angular 9 - dependency injection errors ; Error · This constructor was not compatible ; Injection · Error: ; Dependency...
Read more >
Testing Dependency Injection • Angular - codecraft.tv
When the component is created since it has its own injector it will resolve the AuthService itself and not forward the request to...
Read more >
Testing services - Angular
Your application relies on Angular dependency injection (DI) to create services. When a service has a dependent service, DI finds or creates that...
Read more >
Testing and faking Angular dependencies - DEV Community ‍ ‍
Resolving dependencies using the inject function. The Angular testing utilities give us more than one way to resolve a dependency. In this test, ......
Read more >
Upgrading to Angular 9: My experience | by Bjorn 'Bjeaurn'
I'm still not entirely certain what exactly caused this compilation error after running the upgrade, it may be related to the Angular CLI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found