mardi 1 août 2017

How to test called promises not returned in tested function

first of all, I've been trying almost everything to make this code work, and I achieved, the problem is that I don't like the approach, and I wanted to know if there was something better I could do in order to make the TEST code more readable, but functional.

The main problem here is that "I can't modify the functionToTest" and I have to write tests that would be basically checking that the API calls are being done.

How with the given code would you run the assertions after functionToTest has finished?

PS: The code is shit, I know, but sometimes you just have to deal with it, you can't do more thank just test the shit out of it before refactoring it :(

const firstApiCall = () => {
    return new Promise(function(resolve,reject) {
        setTimeout(() => {
            resolve('firstApiCall success');
        }, 3);
    });
};

const secondApiCall = () => {
    return new Promise(function(resolve,reject) {
        setTimeout(() => {
            resolve('secondApiCall success');
        }, 3);
    });
};

const functionToTest = () => {
    setTimeout(() => {
        firstApiCall().then(result => {
                setTimeout(() => {
                    secondApiCall()
                }, 2)
            })
    }, 15)
};

Basically the code that generates the mocks does something like this, so at the end you have synchronous code

const firstApiCall = () => {
    return {
        then: (cb) => {
            cb('firstApiCall success')
        }
    }
};

Aucun commentaire:

Enregistrer un commentaire