Domanda

I face a strange problem, I wrote multiple tests for my module but the last one is not working. I test an async function, everything work great, except if the test fail. I don't know why but the code after the assert.equal is not executed. If the test succeed, done is correctly called.

describe('download', function(){

    it('should download pictures from given URLs', function(done){
        Download.download(list, function(){

            var pictures = fs.readdirSync("exports/merge/");

            console.log('before assert - '+pictures.length);
            assert.equal(pictures.length, 3);
            console.log('before done - '+pictures.length);
            done();
        });
    });
});

The log 'before assert' is printed, then nothing happen. If I pictures.length equal 3, done() is correctly called.

È stato utile?

Soluzione

Promises swallow errors. In order for them to be thrown, the easiest solution is to end the promise chain with .done().

    var p = Download.download(list, function(){

        var pictures = fs.readdirSync("exports/merge/");

        console.log('before assert - '+pictures.length);
        assert.equal(pictures.length, 3);
        console.log('before done - '+pictures.length);
        done();
    });
    p.done();

See for more information bluebird's documentation or Q's documentation on the subject:

When you get to the end of a chain of promises, you should either return the last promise or end the chain. Since handlers catch errors, it’s an unfortunate pattern that the exceptions can go unobserved. So, either return it or end it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top