0

I have read and tried different things for doing an asynchronous test in jasmine without any success. jasmine: Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

Testing window.postMessage directive

Testing postMessage with Jasmine async doesn't work

I have the following code and i'm receiving the follow output.

Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

    describe('asynchronous tests', function () {
        var value;

        beforeEach(function (done) {
            spyOn(myService.$rootScope, '$broadcast').and.callFake(function(){
                done();
            });
            window.parent.postMessage({message:'event'}, '*');
        });

        it('Should support async execution test preparation and expectation', function(){
            expect(myService.$rootScope.$broadcast).toHaveBeenCalled();
        });
    });

myService is defined in the parent describe function. As I understand my beforeEach should wait until the postMessage is thrown before continuing the execution but I'm getting that timeout error.

Thanks

Community
  • 1
  • 1
acostela
  • 2,597
  • 3
  • 33
  • 50

1 Answers1

1

Finally I simplified my test. I used the eventDispatcher to trigger manually the event that my service is listening and forgetting about window.postMessage(). The reason is what I'm testing is the service not the communication, so it doesn't matter how the event comes.

it('my test', function(){
    var event = new CustomEvent('message');
    event.origin = 'http://localhost:33333';
    event.data = {message: 'eventData'};
    spyOn(myService.$rootScope, '$broadcast');
    myService.subscribeEvent();
    window.dispatchEvent(event);
    expect(myService.$rootScope.$broadcast).toHaveBeenCalledWith('eventData',undefined);
});
acostela
  • 2,597
  • 3
  • 33
  • 50