首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Jest在模板中测试LifeCycles和EvenEmitters

如何使用Jest在模板中测试LifeCycles和EvenEmitters
EN

Stack Overflow用户
提问于 2019-05-16 20:38:34
回答 1查看 1.5K关注 0票数 0

我使用的是Stenciljs和Jest。我很难测试生命周期中触发的事件(componentDidLoad)。

我可以进入生命周期,但无法测试.emit()事件。

我尝试过以下几种方法:

.spec.ts

it('should spyOn componentDidLoad', () => {
   const component = new App();
   jest.spyOn(component, 'componentDidLoad');
    let eventSpy = jest.fn();
    component.document.addEventListener('toScroll', eventSpy);
    expect(component.componentDidLoad()).toHaveBeenCalled();
 });

情况一目了然:

.tsx

  @Event() toScroll: EventEmitter<void>;

  componentDidLoad() {
    this.toScroll.emit();
}

.spec.ts

it('should spyOn componentDidLoad', () => {
    const component = new App();
    jest.spyOn(component, 'componentDidLoad');

    // need a way to test the emit() here.    

    expect(component.componentDidLoad()).toHaveBeenCalled();
  });

出现以下(逻辑)错误:

render component >是否应使用spyOn componentDidLoad

TypeError: Cannot read property 'emit' of undefined
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56168768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档