Angular / Jest / Apollo无法解析组件的所有参数

时间:2019-09-25 13:04:11

标签: angular jestjs apollo

我正在尝试使用依赖于Apollo graphql客户端的Jest测试Angular组件。

我在测试中遇到以下错误:

 SystemStatusComponent
    ✕ should create (518ms)

  ● SystemStatusComponent › should create

    Can't resolve all parameters for SystemStatusComponent: (?).

      at syntaxError (../packages/compiler/src/util.ts:100:17)
      at CompileMetadataResolver._getDependenciesMetadata (../packages/compiler/src/metadata_resolver.ts:957:27)

这是组件:

export class SystemStatusComponent implements OnInit {
  constructor(public apollo: Apollo) {}
  error: any;
  loading = true;
  systemStatuses = [];

  ngOnInit() {
    this.apollo
      .watchQuery({
        query: gql`
          {
            system_status_all {
              code
              verifyString
            }
          }
        `,
      })
      .valueChanges.subscribe((result: any) => {
        console.log('result: ', result);
        this.systemStatuses = result.data && result.data.system_status_all;
        this.loading = result.loading;
        this.error = result.error;
      });
  }
}

这是测试:

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

  beforeEach(async(() => {

    TestBed.configureTestingModule({
      imports: [
        ApolloTestingModule,
      ],
      // providers:[
      //   {
      //     provide: Apollo, // <-- Also fails using this
      //     useValue: jest.fn(),
      //   }
      // ],
      declarations: [SystemStatusComponent],

    }).compileComponents();
  }));

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

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

我还发现包含对ZoneDelegate的引用的错误很有趣:

SystemStatusComponent › should create

    expect(received).toBeTruthy()

    Received: undefined

      28 |
      29 |   it('should create', () => {
    > 30 |     expect(component).toBeTruthy();
         |                       ^
      31 |   });
      32 | });
      33 |

      at it (src/app/pages/admin/system-status/system-status.component.spec.ts:30:23)
      at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:391:26)
      at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:129:39)
      at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:390:52)
      at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:150:43)
      at Object.testBody.length (node_modules/jest-preset-angular/zone-patch/index.js:52:27)

package.json的摘录:

"@angular/core": "~8.2.3",
"apollo-angular": "^1.7.0"
"jest": "^24.9.0",

我按照此处的说明进行操作:https://www.apollographql.com/docs/angular/guides/testing/,但它们似乎不起作用。我怀疑这与Jest专门为TestingModule解决依赖关系有关。我正在使用jest-preset-angular软件包。

我想知道是否有人遇到过同样的问题?请让我知道是否可以提供更多信息。

0 个答案:

没有答案