-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
After upgrade Angular to v8: Can't resolve all parameters for Component: (?). #288
Comments
TL;DRtsconfig.spec.json
"compilerOptions": {
+ "emitDecoratorMetadata": true,
"outDir": "./out-tsc/spec",
IssueThe Angular CLI takes over some of the TS -> JS transpilation using transformers, especially when it's about the decorators and reflection. We were not aware of this change, but it appearently came in in Angular v8.1. Here the full PR: angular/angular-cli#14473 We can try to reuse their transformer (https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/transformers/ctor-parameters.ts), as I will try to experiment with the transformer, we can track the progress in this issue till then. WorkaroundTo make |
@wtho thanks for the quick answer, really appreciated! I've downgraded the reproduction repository to use Angular 8.0.3 and the error is still happening. But the workaround for |
Ok interesting, I created a project with 8.0.0 once and there It's difficult to spot, as I could not find the PR referenced in the angular-cli releases list anywhere. |
Oh alright, that could be it, I've only downgraded the angular dependencies manually in |
So I was able to wrap the transformer, but I am not sure if this is the way to with Why?This example leads to a runtime error in JIT-compiled Angular projects <v7 using @Injectable()
export class BService {
constructor(@Inject(forwardRef(() => AService)) public as: AService) {}
}
@Injectable()
export class AService {
} Runtime Error with More to read about it: angular/angular#30106 and microsoft/TypeScript#27519 Angular AOT always handled this, but JIT only since recently (since this PR angular/angular-cli#14473). Basically a test with this scenario would pass in Angular or karma, but not with our preset. This is achieved using the DecoratorDownlevelTransformer. What does the transformer doThe transformer stores the constructor parameter types as JS values on the object inside a
DiscussionTo make the existing
On the other hand
I also used The wrapper would look something like this: const { readConfiguration } = require('@angular/compiler-cli')
const { downlevelConstructorParameters } = require('@ngtools/webpack/src/transformers/ctor-parameters.js')
const ts = require('typescript')
function factory(cs) {
const config = readConfiguration(cs.tsJest.tsConfig.value)
const program = ts.createProgram(config.rootNames, config.options || {}, config.compilerHost)
return downlevelConstructorParameters(() => program.getTypeChecker())
} I would require more feedback from you guys, @thymikee and especially @ahnpnl or someone else from UpdateI figured out we can create the program with the ts compiler module inside const { downlevelConstructorParameters } = require('@ngtools/webpack/src/transformers/ctor-parameters.js')
function factory(cs) {
const program = cs.compilerModule.createProgram(cs.typescript.fileNames, cs.typescript.options, undefined)
return downlevelConstructorParameters(() => program.getTypeChecker())
} If the TS Program and Type Checker would be instantiated by |
FWIW - I've updated the Briebug schematic to support Angular 8. It includes some of the changes mentioned above in version 2.0.0 - https://github.com/briebug/jest-schematic |
solved my problem,nice |
fix inject problem case like ``` export class AppComponent { constructor(public translate:TranslateService) { // error translate.addLangs(['en', 'fr']); translate.setDefaultLang('en'); const browserLang = translate.getBrowserLang(); translate.use(browserLang.match(/en|fr/) ? browserLang : 'en'); } } ``` it will throw a error Can't resolve all parameters for AppComponent: (?) quote: thymikee/jest-preset-angular#288
The only thing that worked for me, after trying all the other options here,was to enable the following in the compilerOptions in the tsconfig.json file: |
@who I think we can start working on this right ?, based on these codes from you + the exposed Program from ts-jest. Currently isolatedModules mode doesn’t expose Program so I don’t know if it is a must to have or just use compileModule.createProgram as fallback for isolatedModules mode like you did above ? |
@ahnpnl yes, I have a working implementation for
Finally the best solution for everybody would be a performant |
Hmm actually I gonna create a PR to expose I noticed that the impact to performance is usually noticeable when using
Exposing Update 1: I have tested the tgz file from that Update 2: Exposing |
Ok, awesome! This simplifies things for this issue. Will prepare the PR soon! |
The error is fixed using this |
@norato this workaround was mentioned several times before in this thread, but is just a workaround and not more, not a fix for the preset. If you wonder why this is not closed: The fix in this preset would not require any changes in the |
@wtho I did not found those references mentioned when I wrote this comment. This was just to help the ones that need to "fix" this error until we don't have the solution. |
This is a weird one. I'm also getting this error, however, I do have Edit: seems like adding |
@villelahdenvuo Do you have an own file for each injectable class? |
@wtho Yeah, but I actually re-export the class through a another file, that might be why it's failing. |
@wtho I can confirm that moving the failing class to the file being imported made the provider work again even without isolatedModules. |
Hey @Murkhassan86, Try this instead: constructor(private fb: FormBuilder) {} Cheers! |
Dear wtho, |
Why I'm getting this error? "emitDecoratorMetadata": true, didn't work.
There is something wrong with constructor here. |
@senseihimanshu Most likely due to that you're declaring |
This comment has been minimized.
This comment has been minimized.
You are awesome. It solved my issue. Iwas struggling for days and hours. |
Closes #108 Closes #288 Closes #322 Closes #353 Closes #622 BREAKING CHANGE: With the new jest transformer, `jest-preset-angular` now switches to default to use this new transformer and no longer uses `ts-jest` to transform codes. Users who are currently doing in jest config ``` // jest.config.js module.exports = { // [...] transform: { '^.+\\.(ts|js|html)$': 'ts-jest', }, } ``` should change to ``` // jest.config.js module.exports = { // [...] transform: { '^.+\\.(ts|js|html)$': 'jest-preset-angular', }, } ``` `isolatedModule: true` will still use `ts-jest` to compile `ts` to `js` but you won't get full compatibility with Ivy.
Hey there! Any news on this bug? |
Hi folks,
I recently upgraded my application to Angular 8 and couldn't get the tests to run successfully with Jest-Preset-Angular resulting in the error below telling me it cannot resolve the constructor dependencies although it works fine using Karma and was working with Angular 7.
I've created a newly generated Angular 8 application to see if it has something to do with my migration and I ended up having the same issue.
Here is the reproduction repository (Jest configuration is in
package.json
):https://github.com/jfcere/jest-preset-angular-issue
Thanks in advance!
The text was updated successfully, but these errors were encountered: