我使用的是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
https://stackoverflow.com/questions/56168768
复制相似问题