Skip to content
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

False positive ng-mocks not in JIT. #1427

Closed
qdelettre opened this issue Dec 1, 2021 · 81 comments · Fixed by #1547 or #1626
Closed

False positive ng-mocks not in JIT. #1427

qdelettre opened this issue Dec 1, 2021 · 81 comments · Fixed by #1547 or #1626

Comments

@qdelettre
Copy link

Hi !

FYI i just tried to upgrade my toy app to angular v13.0.3, and it seems something is breaking ng-mock (it was working previously in v13.0.2)

Related PR

Actions Job log

@satanTime
Copy link
Member

Hi @qdelettre

Thanks for the info.

I'm on vacation, I'll be back in 1 week and take a look at the issue.

@FunnyGhost
Copy link

Same here. Using it with NX, angular 13.0.3, ng-mocks 12.5.0.

@miniskurken
Copy link

miniskurken commented Dec 6, 2021

Having problems upgrading it to 13.0.3 with ng-mocks 12.5.0 as well.
However can't seem to get it working with 13.0.2 either.

Managed to get 13.0.2 working after a while.

@kamilchlebek
Copy link

kamilchlebek commented Dec 6, 2021

I think one of the problems is here:
https://github.com/ike18t/ng-mocks/blob/d2cf130294b4e9eef5a7efdb55487bed6f6297ce/libs/ng-mocks/src/lib/common/core.reflect.directive-resolve.ts#L1-L7

MockDirectiveResolver was removed in angular 13 (angular/angular@bb9ff60#diff-a276decfccb83a8fb5eae9b051127ce2e3773c9407992869061a648e7bf1d1e5)

@Lutonite
Copy link

Lutonite commented Dec 7, 2021

I've also encountered the issue in a library I'm maintaining. I have a MockBuilder set to mock a module defined with @NgModule in the test file, I've done this to check the implementation of a guard that I provide, so I had to have an example module with some routes to check that the guard is actually working.

Reading the stacktrace suggests that ng-mocks is trying to use MockNgModuleResolver which was removed in @angular/compiler 13.0.3 (where @angular/compiler/testing got completely nuked). Downgrading only that dependency to 13.0.2 fixed the issue for me.

Loving the project so far, I hope that a solution can be found to fix this.

Stacktrace:

Error: RouterModule declaration has been passed into ng-mocks without Angular decorators. Therefore, it cannot be properly handled. Highly likely,  ng-mocks is not in JIT mode. Otherwise, please create an issue on github: https://github.com/ike18t/ng-mocks/issues/new?title=False%20positive%20ng-mocks%20not%20in%20JIT. Thank you in advance for support.
	    at node_modules/ng-mocks/cjs/lib/common/error.missing-decorators.js:5:1
	    at exports.default (node_modules/ng-mocks/cjs/lib/common/core.reflect.body-catch.js:16:1)
	    at node_modules/ng-mocks/cjs/lib/common/core.reflect.module-resolve.js:10:142
	    at getMockModuleDef (node_modules/ng-mocks/cjs/lib/mock-module/mock-module.js:114:1)
	    at detectMockModule (node_modules/ng-mocks/cjs/lib/mock-module/mock-module.js:123:1)
	    at MockModule (node_modules/ng-mocks/cjs/lib/mock-module/mock-module.js:161:1)
	    at node_modules/ng-mocks/cjs/lib/mock-builder/promise/init-modules.js:64:1
	    at node_modules/ng-mocks/cjs/lib/mock-builder/promise/init-universe.js:69:1
	    at MockBuilderPerformance.MockBuilderPromise.build (node_modules/ng-mocks/cjs/lib/mock-builder/mock-builder.promise.js:134:1)
	    at MockBuilderPerformance.build (node_modules/ng-mocks/cjs/lib/mock-builder/mock-builder.performance.js:96:1)

@satanTime
Copy link
Member

Hi all,

I'm back and going to check and fix the issue during this week.
Thank you all for the provided info.

@nicolasstevenyates
Copy link

nicolasstevenyates commented Dec 8, 2021

For me the problem was solved by downgrading to 13.0.2. Thank you for your support.

@stefan-niedermann
Copy link

This is not a solution but a temporary workaround.

@chathurasudarsha
Copy link

I'm having same issue.

@SEQUOiA87
Copy link

Hi all,

I'm back and going to check and fix the issue during this week. Thank you all for the provided info.

Thanks a lot! Shipping a fix for this asap would be greatly appreciated!

@satanTime
Copy link
Member

Hi folks,

I'm working on the fix, but due to core changes in A13 it still requires time to properly fix it in the way that it would support A5+ versions.

Btw, are there devs which don't install @angular/forms or @angular/platform-browser modules?

@patelvimal
Copy link

Thanks @satanTime for the fix.
Lets us know if we can contribute to this in any way possible.

Can't we update the docs that which ng-mock version support which version of angular.
So that it will be easy to consumer as well and easy to maintain as well.

@krusche
Copy link

krusche commented Dec 15, 2021

I'm working on the fix, but due to core changes in A13 it still requires time to properly fix it in the way that it would support A5+ versions.

Can you not release a new major version and drop support for old Angular versions?

@LPCmedia
Copy link

LPCmedia commented Dec 15, 2021

Would it make sense to follow angular LTS ? with 10 ending on dec 24rth .
seems that going back that far is a maintenance nightmare.

https://angular.io/guide/releases#support-policy-and-schedule

is there a release or branch we can use in the meantime for those of us on ng13 ?

@satanTime
Copy link
Member

I'm working on the fix, but due to core changes in A13 it still requires time to properly fix it in the way that it would support A5+ versions.

Can you not release a new major version and drop support for old Angular versions?

Yes, this is what I'm checking currently. The issue is that it's not purely A13 change, as I understand <=13.0.2 uses the old approach from A5 and all younger versions use the new one, so even if I release a new major version it should still support both approaches.

Anyway the solution will be found. Either with support of all A version or just A13.

@dolanmiu
Copy link

Is there a rough time frame for when this would be fixed?

@satanTime
Copy link
Member

Hi,

the estimate is asap.
The last weekend, I almost solved dependencies to bring the repo to latest versions.

The next weekend, I should finish that and to switch to investigation and implementation of a fix. It may take 2 more weekends, and considering the holiday, I would expect a properly working fix around mid January.

Currently, I would recommend to stay on Angular 13.0.2 and ng-mocks 12.5.0.

Unfortunately, this task isn't something where it's easy to help.

If anyone has time, it would be great to fix the PR with the update to the latest jest before the upcoming weekend.

@Fafnur
Copy link

Fafnur commented Dec 20, 2021

@satanTime Hi.
Can you release a "alpha" version, which will work > A13.02. You don't need to merge this version on master, it's only hotfix for all us, who using > A13.0.2.

@satanTime
Copy link
Member

To release an alpha candidate, I still need to bring the repo to the latest dependencies, to ensure that the candidate will work with all of them, because otherwise - Angular 13 is supported, but the latest jest version is failing, and it will be the same story.

Might you describe, why you have to stay with >13.0.2 and cannot downgrade to 13.0.2 until the fix has been released?

Also for all people who want to help with the project - feel free to take any good first issue, it will help the project a lot too.

Thank you all in advance.

@markchagers
Copy link

markchagers commented Dec 23, 2021

I just downgraded an angular 13.1.2 project to 13.0.2 and the same error still occurs!
This is not a big problem for me currently, but I thought it would be useful to know that downgrading an existing project to angular 13.0.2 does NOT solve the issue.

@SEQUOiA87
Copy link

I just downgraded an angular 13.1.2 project to 13.0.2 and the same error still occurs! This is not a big problem for me currently, but I thought it would be useful to know that downgrading an existing project to angular 13.0.2 does NOT solve the issue.

That sounds weird. For us it definitely did the trick.

Please make sure that in your .lock file @angular/compiler is not listed in version 13.0.3 (or above) anymore.

@waratah
Copy link

waratah commented Dec 23, 2021

even though I listed the correct version in package, package lock was up a few versions. I removed node_modules and reinstalled from scratch to get it right from memory. downgrade can be fiddly.

@FrancescoBorzi
Copy link

confirmed

@markchagers
Copy link

markchagers commented Dec 27, 2021

actually I first uninstalled @angular/cli globally and re-installed as follows: npm i @angular/cli@13.0.2.
Then I created a new project, deleted package.lock file and node_modules from my existing project, and manually copied all version numbers from the new project to package.json and did an npm install. (this was my umpteenth attempt at downgrading that project).
However, although ng version now returns 13.0.2 globally (in my home dir), inside the project directory it returns 13.0.4 (go figure). Apparently it is not humanly possible to downgrade an angular project to any specific version.
Anyway, I will wait until this ngMocks issue is fixed before updating my main project from angular 12 (I was experimenting with a test project until now)
@dolanmiu any rational argument for the downvote?

@SnakeSVx
Copy link

SnakeSVx commented Jan 18, 2022

@satanTime pipes seems to be fixed :)

Regarding the ID, it's the same components as before again. I'm guessing it's the hostbinding in them that's causing it.

@Component({
  selector: 'our-custom-selector',
  templateUrl: './custom-form.html',
  styleUrls: ['./custom-form.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: [
    {
      provide: MatFormFieldControl,
      useExisting: CustomFormComponent
    }
  ]
})
export class CustomFormComponent implements OnInit, AfterViewInit,
  ControlValueAccessor, MatFormFieldControl<Number>>, OnDestroy, AfterContentChecked {
  
  static nextId = 0;
  
  @HostBinding()
  id = `custom-form-${++CustomFormComponent.nextId}`;
  
  ...
  
 }

If I disable that HostBinding the test succeeds.

In the html it added this to t he mocked component: id="undefined"

@satanTime
Copy link
Member

Hi @SnakeSVx,

Yes. I've added host bindings and host listeners.

Now I need to think whether we need them in mocked declarations. And, knowing that nobody has complained about them for years - looks like it was a mistake :)

@SEQUOiA87
Copy link

FYI: It seems we were also affected by the problem with mocking pipes, now with alpha.5 it's fixed.

Thanks a lot for your work!

@MattBrou
Copy link

You're a life saver @satanTime. Thanks for you great work.

satanTime added a commit to satanTime/ng-mocks that referenced this issue Jan 18, 2022
satanTime added a commit to satanTime/ng-mocks that referenced this issue Jan 18, 2022
satanTime added a commit to satanTime/ng-mocks that referenced this issue Jan 18, 2022
satanTime added a commit to satanTime/ng-mocks that referenced this issue Jan 18, 2022
satanTime added a commit to satanTime/ng-mocks that referenced this issue Jan 18, 2022
satanTime added a commit that referenced this issue Jan 18, 2022
fix(core): ignoring host bindings in mocks #1427
@satanTime
Copy link
Member

v13.0.0-alpha.6 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@satanTime
Copy link
Member

satanTime commented Jan 18, 2022

Hi @SnakeSVx, please check the latest release. Hopefully, it brings all your tests to the green state again.

The next step is to verify the issue with animations: #1586 and then to release a stable version.
if anyone is facing any issues - feel free to drop a line here.

Thank you all.

@Plondrein
Copy link

All green now, thanks a lot !

@SnakeSVx
Copy link

SnakeSVx commented Jan 19, 2022

Same, all green for us now as well.

Thank you for all the help and providing us with this great library :)

@satanTime
Copy link
Member

Awesome! Thank you all for help and feedback.

Stay in touch 🤙🏼

@cdupetit
Copy link

cdupetit commented Jan 20, 2022

Hello @satanTime,
I get this warning with a mat-checkbox component when I run my jest test with ng-mocks v13.0.0-alpha.2.

  console.error
    NG0303: Can't bind to 'ngIf' since it isn't a known property of 'div'.

      at logUnknownPropertyError (node_modules/@angular/core/fesm2015/core.mjs:10119:13)
      at elementPropertyInternal (node_modules/@angular/core/fesm2015/core.mjs:10031:13)
      at ɵɵproperty (node_modules/@angular/core/fesm2015/core.mjs:14431:9)
      at MockOfMatCheckbox_Template (ng:/MockOfMatCheckbox.js:65:9)
      at executeTemplate (node_modules/@angular/core/fesm2015/core.mjs:9593:9)
      at refreshView (node_modules/@angular/core/fesm2015/core.mjs:9459:13)
      at refreshComponent (node_modules/@angular/core/fesm2015/core.mjs:10

Best Regards

@satanTime
Copy link
Member

Hi @cdupetit,

Could you try .6?

@cdupetit
Copy link

Hi @cdupetit,

Could you try .6?

This version fix the warning.
Thanks !

@maxs-rose
Copy link

.6 is working for us with Angular 13 and Kendo UI

@satanTime
Copy link
Member

v13.0.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@squelix
Copy link

squelix commented Jan 26, 2022

Everything is working fine now !! :) Thanks !!!!

@Rlcolli4
Copy link

I just set this up on our unit tests and things are working great. Thank you so much for your hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet