Events in Unit Testing


#1

I’m buildind a small component with Stencil. I need to validate the given props and emit an event if they are invalid on component creation. In my component class I have:

@Event() invalidSpecs: EventEmitter;

componentWillLoad() {
  let specs = this._getSpecs();
  if(specs.invalid)
    this.invalidSpecs.emit(specs);
}

and in my specs:

it('emits invalidSpecs event', async done => {
    let element: HTMLSpecsItemElement;
    let testWindow = new TestWindow(); 

    testWindow.document.addEventListener("invalidSpecs", (specs) => {
      done();
    });   
    
    element = await testWindow.load({
      components: [SpecsItem],
      html: '<specs-item></specs-item>'
    });       
});

The problem is that Jest fails because done() never gets called. I tested it on the server by putting a listener in a script tag in index.html and the event does get fired. So I suppose that I’m doing something wrong in my spec code. I hope you guys can put me in the right path.