2000 ms timeout exceeded mocha

I have two test cases, i.e. he ("must pass with ..") .. and he ("must fail with ..") .. When I test this, he gave a timeout of over 2000 ms.

describe("flickrphotoSearch", function () {
it("should pass with correct inputs", function (done) {
    flickrApplication.flickrPhotoSearch("hello", "flickr_user_Key", 1, handleData);
    function handleData(photoUrl, done) {
        this.setTimeout(1500);
        assert.isString(photoUrl.toString(), 'not a string');
        setTimeout(done, 1000);
    };
});
it("should fail with wrong key", function (callingDone) {
    flickrApplication.flickrPhotoSearch("hello", "wrong key", 1, handleData);
    function handleData(photoUrl, done) {
        this.setTimeout(1500);
        assert.equal(photoUrl.stat, "ok", photoUrl.message);
        setTimeout(done, 1000);
    };
});
});

for the first test, I get a timeout for the exceeded error, but the second works well. Please tell me where I am going wrong.

+4
source share
1 answer

There are two parts. First, when you try to set a timeout for a test, you do not call the method setTimeouton the desired object. This is due to the closure:

describe("flickrphotoSearch", function () {
it("should pass with correct inputs", function (done) {
    # 'this' is the mocha test here.
    flickrApplication.flickrPhotoSearch("hello", "flickr_user_Key", 1, handleData);
    function handleData(photoUrl, done) {
        this.setTimeout(1500); # What 'this' here? The global object.
        assert.isString(photoUrl.toString(), 'not a string');
        setTimeout(done, 1000);
    };
});

handleData, this , , . this . jQuery Learning Center. , :

flickrApplication.flickrPhotoSearch("hello", "flickr_user_Key", 1, handleData.bind(this));

this.setTimeout(1500) handleData, .

, , 2000 , 1500 . , , API flickr.

, API flickr, unit test ( ).

+1

Source: https://habr.com/ru/post/1536082/


All Articles