Why is this async Jasmine test failing?

Why is this test failing, saying that the onSuccess spy was never called?

it('should correctly call the success callback',function(done)
{
    const callbacks={
        onSuccess:function()
        {
            console.log('OK');
            done();
        },
        onError:function()
        {
            console.log('ERR');
            done();
        }
    };

    spyOn(callbacks,'onSuccess').and.callThrough();
    spyOn(callbacks,'onError').and.callThrough();

    doSomethingAsync(callbacks.onSuccess,callbacks.onError);

    expect(callbacks.onSuccess).toHaveBeenCalled();
    expect(callbacks.onError).not.toHaveBeenCalled();
});

When running the test, I get an error saying Expected spy onSuccess to have been called.. Right above that, there's a console log saying "OK", meaning that the spy was called and that it called through.

Answers


It's difficult to assert that one callback is called and another one isn't without stubbing doSomethingAsync or its internals.

Here's a contrived edge case:

function doSomethingAsync(onSuccess, onError) {
  setTimeout(function() {
    onSuccess('hello');
  }, 500);
  setTimeout(function() {
    onError(new Error('foo'));
  }, 1000);
}

(so it calls onSuccess after half a second, and onError after a second)

If you assert, in the onSuccess handler, that onError hasn't been called, the test will pass, even though onError is getting called (albeit half a second later).

This is something that you cannot easily work around, unless (as stated before) you stub (the internals of) doSomethingAsync.

If you simply want to test if doSomethingAsync calls the right handler, you can shorten your test case to this (provided that it's not strictly necessary to call the handlers in your callbacks object):

it('should correctly call the success callback',function(done) {
  doSomethingAsync(done, done.fail);
});

(this doesn't catch doSomethingAsync calling both handlers, though; if it calls onSuccess before onError, the test will pass).


Need Your Help

How to list all AWS S3 objects in a bucket using Java

java amazon-s3

What is the simplest way to get a list of all items within an S3 bucket using Java?