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

entryComponents required in tests #17110

Closed
pburkindine opened this issue Feb 19, 2020 · 14 comments
Closed

entryComponents required in tests #17110

pburkindine opened this issue Feb 19, 2020 · 14 comments

Comments

@pburkindine
Copy link

We are having to declare entryComponents in tests after upgrading to A9; the documentation indicates this should not be necessary


@NgModule({
  entryComponents: [
    xyz,
  ]
})
export class ImportModule {}
 beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule, ImportModule],
...
@pacocom
Copy link

pacocom commented Feb 19, 2020

I have the same problems!!!

@pburkindine
Copy link
Author

pburkindine commented Feb 19, 2020

The entryComponents in question are all present in the declarations in the TestBed btw

@AndrewKushnir
Copy link

Hi @pburkindine, could you please provide a minimum repro (in a form of GitHub repo), so that we can perform further investigation? Thank you.

@pacocom
Copy link

pacocom commented Feb 20, 2020

Hello!

I have a example:

import { HttpClientTestingModule } from '@angular/common/http/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatBottomSheetModule, MatBottomSheetRef } from '@angular/material/bottom-sheet';
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { SharedModule } from '@shared';
import { CommonTermsModule } from '@shared-components';
import { DownloadUserDataComponent } from '../download-user-data';
import { DownloadModalComponent } from './download-modal.component';

const mockActivatedRoute = {
  parent: {},
};

describe('DownloadModalComponent', () => {
  let component: DownloadModalComponent;
  let fixture: ComponentFixture<DownloadModalComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [DownloadModalComponent, DownloadUserDataComponent],
      imports: [
        NoopAnimationsModule,
        RouterTestingModule,
        HttpClientTestingModule,
        MatBottomSheetModule,
        SharedModule,
        CommonTermsModule,
        ReactiveFormsModule,
        FormsModule,
      ],
      providers: [{ provide: ActivatedRoute, useValue: mockActivatedRoute }, { provide: MatBottomSheetRef }],
    })
      // TODO: Remove next line when we know how Angular 9 test entryComponents
      // IF I REMOVE THIS LINE TEST BREAK
      .overrideModule(BrowserDynamicTestingModule, { set: { entryComponents: [DownloadUserDataComponent] } })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DownloadModalComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

If I remove next line test ERROR!
.overrideModule(BrowserDynamicTestingModule, { set: { entryComponents: [DownloadUserDataComponent] } })

Thank!

@AndrewKushnir
Copy link

Hi @pacocom, thanks for additional information.

Could you please create a GitHub repo with a minimum possible repro (I assume some of the modules in imports section and providers may not affect), so that we can perform further investigation?

I also want to confirm if you use Ivy to reproduce the problem (looks like the error message was produced by View Engine)?

Thank you.

@pburkindine
Copy link
Author

@AndrewKushnir I have not had time this work week but I will try to repro this weekend; I wanted to make sure it was on the books

@archasek
Copy link

archasek commented Feb 27, 2020

Hi @AndrewKushnir, I guess I have a similar problem here.

The app works ok without entryComponents, but tests don't.

https://github.com/archasek/angular-playground

Crucial line: https://github.com/archasek/angular-playground/blob/master/src/app/app.component.spec.ts#L32

Just run tests with npm test.

Thanks

@AndrewKushnir
Copy link

Hi @archasek, thanks for the repository with the setup that triggers the issue.

I checked error logs and found out that ViewEngine is used to run the tests and that's the reason why entryComponent issue happens. I looked at the package.json file and found out that you configured your project to use Jest and it looks like additional configuration might be required.

I'm transferring this issue to Angular CLI repo for further investigation.

Thank you.

@AndrewKushnir AndrewKushnir transferred this issue from angular/angular Feb 28, 2020
@alan-agius4
Copy link
Collaborator

Hi, the problem with Jest is that it doesn't use @ngtools/webpack to bundle and resolve the packages but rather rely on Node.Js resolutions. These modules are not being transformed using NGCC and thus modules are resolved to their VE version.

You will need to run ngcc manually as a postinstall hook so that all modules are transformed to be Ivy compatible.

"postinstall": "ngcc --properties main"

@archasek
Copy link

archasek commented Feb 28, 2020

@alan-agius4 thanks so much, works like a charm for a demo repo.
Thanks @AndrewKushnir !

Btw. for those who are experiencing errors with $localize() in Jest tests after this change: thymikee/jest-preset-angular#347 (comment)

@pburkindine
Copy link
Author

pburkindine commented Mar 17, 2020

@alan-agius4
I was able to get things going by replacing the recommended postinstall (ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points) with ngcc --properties main. In particular, I had to remove --create-ivy-entry-points to get this to work. I am wondering what the implications of turning that flag off are.

UPDATE: I changed the postinstall to ngcc --properties es2015 as the main target was compiling to UMD. This caused the tests to fail as well (either main with --create-ivy-entry-points or es2015 target will cause the entryComponents error).

@pburkindine
Copy link
Author

@archasek thanks for the tip on the $localize setup issue which was also required to get things going

@pburkindine
Copy link
Author

@alan-agius4 actually I spoke too soon, simple tests that e.g. a destroy$ subject is stopping some ngrx pipe fail when using the main/UMD target which weren't failing before

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants