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
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
A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.
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:
Add any other context about the problem here.
Does running ngcc command before running the tests help?
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:
```Error: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
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.```
You need to either import your module in imports array or provide the service within the providers array of the testing module. Otherwise the TestBed wouldn't know that your service is part of DI mechanism.
Please refer to the official guide for more info.
Please update if I can close the issue.
I have indeed tried both these methods before opening an issue, neither of those did the trick.
I implemented my test example the exact same way I do on my regular projects, and I have never had any such problem before.
Have you tried cloning my example repo and running ng test against it ?
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.
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.```
@michaelfaith Gonna need a repro for this.
@just-jeb After spending two days trying to get to the root cause, i think i finally figured it out, though i'm not entirely sure why. I created a brand new project and tried to reproduce it, but wasn't having any luck. I slowly migrated everything one thing at a time from my existing project, and still no luck reproducing it. Until the directories were exactly the same (or so i thought). Turns out the line returns seem to be a problem. I'm on windows, but i keep my existing project using unix style line returns (lf). The new project i created to try and reproduce, created it using crlf windows line returns. When i convert my existing project to crlf it suddenly started working, and that error went away. That definitely seems like a bug to me. Now that i discovered that, i could probably create a repro repo, if you need, but it should be as simple as changing a project to LF line returns on Windows. Or just a few of the spec files.
And just to follow-up. I changed this project over to use the different pieces separately (jest-preset-angular, @types/jest, and jest) instead of using the builder, and the jest runs work fine regardless of line returns. So definitely a bug here. Should i open a new issue?
@michaelfaith Totally yes. A repository with simple reproduction could speed it up. Thank you.
Most helpful comment
Yes, I cloned, indeed there was a problem.
You need to set
"emitDecoratorMetadata": trueincompilerOptionsof yourtsconfig.spec.json. Once you do this it starts working.More details here.
I'll update the article.