Search code examples
javascriptinternleadfoot

executeAsync not passing return value to callback


I'm using intern JS/leadfood testing framework. I'm using executeAsync. I expect the return value from executeAsync to be passed to the callback to executeAsync, but this is not happening. Should the following work?

return  this.remote.get(require.toUrl(url));
    //do stuff
    .executeAsync(function (done) {

        require([<library>],
            function ([<function>]) {
                return <function which returns Promise>
                .then(function (value) {
                    return <function which returns Promise>
                ...
                }).then(function () {
                    done(window.location);

                })
            })

    })
    .then(function (loc) {
        console.log(loc);
    })

Execution gets to the last callback in executeAsync successfully. The callback to executeAsync is called successfully. But the value passed to the executeAsync callback is undefined.

Edit: I have found out that even if you set a executeAsync timeout of a very large number, this timeout will be ignored if you do not call this.async(timeout) specifying the correct timeout (default is 30 seconds at time of writing). So the issue was that the test was taking longer than 30 seconds and the value passed to done was not making it to the callback to executeAsync.


Solution

  • executeAsync uses a callback to determine when its function has finished running. This callback is automatically passed as the last argument (the only argument if you're not passing anything else) to the executeAsync function:

    define([
        'require',
        'intern!object'
    ], function (
        require,
        registerSuite
    ) {
        registerSuite({
            name: 'test',
    
            foo: function () {
                return this.remote.get(require.toUrl('./index.html'))
                    .setExecuteAsyncTimeout(5000)
                    .executeAsync(function (done) {
                        var promise = new Promise(function (resolve) {
                            setTimeout(function () {
                                resolve();
                            }, 1000);
                        });
    
                        promise.then(function () {
                            return new Promise(function (resolve) {
                                setTimeout(function () {
                                    resolve();
                                }, 1000);
                            });
                        }).then(function () {
                            done(window.location);
                        });
                    })
                    .then(function (loc) {
                        // This prints out a full location object from the
                        // browser.
                        console.log(loc);
                    }); 
            }
        });
    });