ngneat / until-destroy

šŸ¦Š RxJS operator that unsubscribe from observables on destroy
https://netbasal.com/
MIT License
1.74k stars 100 forks source link

Unable to test when mocking decorated component via MockComponent #144

Closed krzyhan closed 3 years ago

krzyhan commented 3 years ago

Hi, Is there any workaround to deal with an error while testing with spectator & ng-mocks?

Child component has a decorator @UntilDestory() and when I use MockComponent(ChildComponent) I cannot run test. Getting error like with disabled Ivy (but Ivy is enabled). Cannot read property 'get' of undefined

Sth like this:

describe('ParentComponent', () => {
 let spectator: Spectator<ParentComponent>;

 const createComponent = createComponentFactory({
    component: ParentComponent,
    detectChanges: false,
    declarations: [MockComponent(ChildComponent)]
  });

 beforeEach(() => {
    spectator = createComponent();
  });

 test('should be created', () => {
   expect(spectator.component).toBeTruthy();
 });
});

@Component({
  selector: 'parent',
  template: '<child></child>'
})
export class ParentComponent {}

@UntilDestroy()
@Component({
  selector: 'child',
  template: 'anything'
})
export class ChildComponent {}

If I use shallow: true w/out using MockComponent() everything is working fine. Any idea?

   "@angular/common": "~9.1.11",
   "@angular/compiler": "~9.1.11",
   "@angular/core": "~9.1.11",
   "@ngneat/until-destroy": "^7.3.2",
   "@ngneat/spectator": "^5.13.1",
   "jest": "^26.4.2",
   "ng-mocks": "^10.1.2",
arturovt commented 3 years ago

@krzyhan could you provide a minimal reproducible example? This error is thrown Cannot read property 'get' of undefined when Ivy is actually disabled and it can be disabled in testing environment.

NetanelBasal commented 3 years ago

I'm closing the issue. If you can reproduce it, let us know.

krzyhan commented 3 years ago

Sorry guys, I updated Angular & and all dependencies to the latest version and it's working fine.

Thanks for replying.