类型 X 是 2 个模块声明的一部分:Y 和 Y!...在 Angular Jest 测试中

sno*_*dev 5 typescript jestjs angular ts-jest

运行 Angular Jest 测试时,出现以下错误:

"Type InButtonComponent is part of the declarations of 2 modules: InButtonModule and InButtonModule! Please consider moving InButtonComponent to a higher module that imports InButtonModule and InButtonModule. You can also create a new NgModule that exports and includes InButtonComponent then import that NgModule in InButtonModule and InButtonModule."
Run Code Online (Sandbox Code Playgroud)

我以前遇到过“2 个模块的声明”问题,通常很容易解决;不要在两个不同的模块中声明相同的组件。但是,在这种情况下,我不会InButtonComponent在两个不同的模块中声明。就像 Angular 抱怨我在同一个模块中声明了两次组件?该错误消息对我来说没有任何意义。

无论我是为产品构建还是运行开发服务器,我都不会在应用程序中收到此错误。然而,当我运行 Jest 测试时,它们失败了。

如果有帮助的话,这里是一些可能涉及的文件:

// button.component.ts  single file SCAM
// ...
@NgModule({
  declarations: [InButtonComponent],
  imports: [CommonModule],
  exports: [InButtonComponent],
})
export class InButtonModule {}
Run Code Online (Sandbox Code Playgroud)
// list-filter.component.ts single file SCAM
// ...
@NgModule({
  imports: [CommonModule, InIconModule, InButtonModule],
  declarations: [InListFilterComponent],
  exports: [InListFilterComponent],
})
export class InListFilterComponentModule {}
Run Code Online (Sandbox Code Playgroud)
// listing.module.ts Feature module
// ...
@NgModule({
  declarations: [
    ListingComponent,
    CardGroupedComponent,
    CardViewComponent,
    CardCommonFeatureComponent,
    QuickFiltersComponent,
    ListNotGroupedComponent,
    ListGroupedComponent,
    ListingHeaderComponent,
    ListingListComponent,
    ListingDetailComponent,
    FilterComponent,
    TableViewComponent,
    DetailViewListsComponent,
  ],
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    InDividerModule,
    InIconModule,
    InCheckboxModule,
    InListingsStatusLabelComponentModule,
    InButtonModule,
    InToggleComponentModule,
    InCarouselModule,
    InFormFieldModule,
    InListFilterComponentModule,
    ListingRoutingModule,
  ],
  providers: [{ provide: QUICK_FILTER_SERVICE, useExisting: ListingQuickFiltersService }],
})
export class ListingModule {}
Run Code Online (Sandbox Code Playgroud)
// filter.component.spec.ts 
// one of the tests that fail with the above error message
describe('FilterComponent', () => {
  let component: FilterComponent;
  let fixture: ComponentFixture<FilterComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [ListingModule, HttpClientTestingModule, RouterTestingModule],
      providers: [DatePipe],
    }).compileComponents();
  });

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

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

Run Code Online (Sandbox Code Playgroud)

造成此问题的原因是什么以及如何解决?