最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Importing standalone component in TestBed not working - Stack Overflow

programmeradmin0浏览0评论

I'm upgrading my app from Angular 17 to Angular 19 (19.0.6) and nx20. I encountered some problems with TestBed and standalone component.

I have the following code :

@Component({
  templateUrl: './connectionponent.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false
})
export class ConnectionComponent implements OnInit {...
beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [FakesModule, CoreTranslationTestingModule, MockComponent(FeatureConsentsComponent)],
      declarations: [
        ConnectionComponent,
        MockComponent(ButtonComponent),
        MockComponents(MotivationBlockComponent, MotivationBlockItemComponent)
      ],
      providers: [
        provideMockStore(),
        provideDummyTokens(),
      ]
    });
    fixture = TestBed.createComponent(ConnectionComponent);
    component = fixtureponentInstance;
  });

The FeatureConsentsComponent is a standalone component :

@Component({
    selector: 'consents',
    templateUrl: './consentsponent.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    imports: [NgIf, CheckBoxModule, LayoutModule, NgFor, AsyncPipe, TranslateModule],
    providers: [ConsentsHttpService, addDynamicComponentProviders(ConsentsErrorsProvider)]
})

But I have the following error when I try to run the test file :

    Unexpected "MockOfFeatureConsentsComponent" found in the "declarations" array of the "TestBed.configureTestingModule" call, "MockOfFeatureConsentsComponent" is marked as standalone and can't be declared in any NgModule - did you intend to import it instead (by adding it to the "imports" array)?

If I force standalone: true on the FeatureConsentsComponent it works. Why ?

I'm upgrading my app from Angular 17 to Angular 19 (19.0.6) and nx20. I encountered some problems with TestBed and standalone component.

I have the following code :

@Component({
  templateUrl: './connectionponent.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false
})
export class ConnectionComponent implements OnInit {...
beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [FakesModule, CoreTranslationTestingModule, MockComponent(FeatureConsentsComponent)],
      declarations: [
        ConnectionComponent,
        MockComponent(ButtonComponent),
        MockComponents(MotivationBlockComponent, MotivationBlockItemComponent)
      ],
      providers: [
        provideMockStore(),
        provideDummyTokens(),
      ]
    });
    fixture = TestBed.createComponent(ConnectionComponent);
    component = fixtureponentInstance;
  });

The FeatureConsentsComponent is a standalone component :

@Component({
    selector: 'consents',
    templateUrl: './consentsponent.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    imports: [NgIf, CheckBoxModule, LayoutModule, NgFor, AsyncPipe, TranslateModule],
    providers: [ConsentsHttpService, addDynamicComponentProviders(ConsentsErrorsProvider)]
})

But I have the following error when I try to run the test file :

    Unexpected "MockOfFeatureConsentsComponent" found in the "declarations" array of the "TestBed.configureTestingModule" call, "MockOfFeatureConsentsComponent" is marked as standalone and can't be declared in any NgModule - did you intend to import it instead (by adding it to the "imports" array)?

If I force standalone: true on the FeatureConsentsComponent it works. Why ?

Share Improve this question edited Mar 28 at 15:15 fservantdev 85114 silver badges21 bronze badges asked Jan 17 at 16:48 PakPak 2,7763 gold badges22 silver badges29 bronze badges 2
  • What is MockComponents ? Could you please fork stackblitz/edit/ng-jasmine and repro your issue ? – Matthieu Riegler Commented Jan 17 at 17:50
  • Additionaly, the component config of ConnectionComponent would be helpful as well. I believe MockComponent might only be working when the passed Component is standalone and is configured inside "imports" both in the test and the tested component (ConnectionComponent in this case) itself? – Loop Commented Jan 18 at 13:46
Add a comment  | 

1 Answer 1

Reset to default 0

I suppose that you use the lib ng-mocks.

The problem that you describe is exactly the one of this issue: https://github/help-me-mom/ng-mocks/issues/10632

Upgrading ng-mocks to 14.13.2 should solve your problem

发布评论

评论列表(0)

  1. 暂无评论