测试时无法导入ng-zorro-antd模块

发布于 2025-01-17 07:59:56 字数 1229 浏览 2 评论 0原文

我无法在单元测试中从 ng-zorro-antd 导入模块,但我可以构建应用程序而不会出现任何错误。我正在使用 Angular 12.2.4 和 ng-zorro-antd 12.1.1

我收到以下错误:

Error: Unexpected value 'NzButtonModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.

这是我的规范文件:

import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import {NzButtonModule} from 'ng-zorro-antd/button';

import { AntViewComponent } from './ant-view.component';

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

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [NzButtonModule],
        declarations: [AntViewComponent],
        schemas: [NO_ERRORS_SCHEMA]
      }).compileComponents();
    })
  );

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

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

我做错了什么吗?这很奇怪,因为我可以很好地运行应用程序,导入和使用 ng-zorro-antd 中的模块,但我不能在单元测试中。

I cannot import modules from ng-zorro-antd in unittests, but I can build the app without any error. I'm using Angular 12.2.4 and ng-zorro-antd 12.1.1

I get the following error:

Error: Unexpected value 'NzButtonModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.

Here my spec file:

import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import {NzButtonModule} from 'ng-zorro-antd/button';

import { AntViewComponent } from './ant-view.component';

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

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [NzButtonModule],
        declarations: [AntViewComponent],
        schemas: [NO_ERRORS_SCHEMA]
      }).compileComponents();
    })
  );

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

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

Did I do something wrong? This is very weird since I can run the app fine, import and use the modules from ng-zorro-antd but I can't in unittests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-01-24 07:59:56

使用旧版本的 ng-zorro-antd 解决了该问题(12.0.1)。

Using an older version of ng-zorro-antd resolved the issue (12.0.1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文