0
votes

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

1
You need to pass done: describe('asynchronous tests', function(done){and I would move whole beforeEach code (spyOn and window.parent) to it as there is only one test.Cezary Tomczyk
I can't add done to the describe I will get a "describe does not expect a done parameter" erroracostela
You should not have such error. See: jasmine.github.io/2.0/…Cezary Tomczyk
I know that's the reason why I'm asking this :)acostela
Ay, my mistake. It should be passed to it, not to describe. :-)Cezary Tomczyk

1 Answers

1
votes

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);
});