简体   繁体   English

使用Jest和Spectator使用反应形式作为输入对子组件进行单元测试时,如何解决此错误?

[英]How do I solve this error when unit testing child component with reactive form as input using Jest and Spectator?

I'm trying to write a basic unit test for an Angular component which is used as a child component and receives a reactive form through an Input() from it's parent. 我正在尝试为用作子组件的Angular组件编写基本的单元测试,并通过其父级的Input()接收反应形式。 I'm trying to write a unit test using a host component to wrap the component to be tested. 我正在尝试使用主机组件编写一个单元测试,以包装要测试的组件。 I'm using https://netbasal.gitbook.io/spectator/ with Jest to write my tests. 我将https://netbasal.gitbook.io/spectator/与Jest一起使用来编写测试。 I can't get the simplest test working for this component and my guess is the problem has something to do with dependencies. 我无法针对该组件进行最简单的测试,我的猜测是问题与依赖关系有关。 I've been searching google extensively but I can't seem to find anybody else running into this error. 我一直在广泛搜索google,但似乎找不到其他人遇到此错误。

I've tried writing the test without the host component but that also doesn't seem to be working. 我试过在没有主机组件的情况下编写测试,但这似乎也不起作用。

describe('ProjectFormComponent', () => {
  let host: SpectatorWithHost<ProjectFormComponent>;

const createHost = createHostComponentFactory<ProjectFormComponent>({
    component: ProjectFormComponent,
    imports: [
      CommonModule,
      ReactiveFormsModule,
      FontAwesomeModule,
      HttpClientTestingModule,
      RouterTestingModule,
      NgxErrorsModule,
      NgSelectModule,
      ToastrModule.forRoot(),
      MaterialImportsModule,
      FormsModule,
      SharedModule
    ],
    providers: [
      { provide: NgxErrorDirective, useValue: {} },
      { provide: APP_BASE_HREF, useValue: '/' },
      { provide: Store, useValue: {} },
      { provide: MAT_DATE_LOCALE, useValue: 'en-GB' },
      { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
      { provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS }
    ]
  });

test('ProjectFormComponent is created', inject([FormBuilder], (fb: FormBuilder) => {
    const mockForm = fb.group({
      name: ['', [Validators.required, Validators.maxLength(255)]]
    });

    host = createHost(`<app-project-form></app-project-form>`, false, { parent_form: mockForm });

    expect(host.component).toBeTruthy();

  }));
});

It's a simple test which should result positive. 这是一个简单的测试,应该会产生积极的结果。 But when I run ng test I get the error: Cannot override module metadata when the test module has already been instantiated. 但是,当我运行ng test时,出现错误:测试模块已经实例化时,无法覆盖模块元数据。 Make sure you are not using inject before overrideModule . 确保您在overrideModule之前没有使用inject

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有@Input / @Output和反应形式的单元测试组件 - Unit testing component with @Input / @Output and reactive form Angular中的单元测试(Jest + Spectator):第二项测试总是失败吗? - Unit testing in Angular (Jest+Spectator): Second test always fails? 使用反应式表单时如何确定子组件形式在父组件中是否有效 - How to determine if child component form is valid in parent component when using reactive forms 为使用 routerLink、Spectator 和 Jest 的角度组件编写测试时出现 `TypeError:将循环结构转换为 JSON` 错误 - `TypeError: Converting circular structure to JSON` error when writing tests for an angular component that utilizes routerLink, Spectator, and Jest 当我尝试访问反应形式的元素进行单元测试时,我收到此错误“TypeError:this.reCaptchaApi.render is not a function” - I am getting this error "TypeError: this.reCaptchaApi.render is not a function" when I try to access elements of reactive form for unit testing 使用 cdkFocusInitial 运行材质对话框组件的开玩笑单元测试时,如何修复“[cdkFocusInitial]”不可聚焦警告? - How do I fix the '[cdkFocusInitial]' is not focusable warning, when running jest unit test of a material dialog component with cdkFocusInitial? 使用Jest进行无法理解的单元测试效果错误 - Ununderstandable unit testing effects error using Jest 使用 Jest 和 Spectator 测试 Angular 拦截器的响应 - Testing Angular interceptors response with Jest and Spectator 在父组件中使用子组件时出错 - Angular 2单元测试(KarmaJasmine) - Error while using child component in parent component - Angular 2 Unit Testing (KarmaJasmine) 补丁反应形式的错误子组件输入不会在 Oninit 上更新 - Bug Child component input to patch reactive form does not update on Oninit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM