jeudi 25 février 2016

superagent + nock, unit testing breaks when adding //

I have been working with some unit tests for my app and have had to change a url which has seem to have broken my tests and I cannot for the life of me figure out why. In short, I have this function which returns me a promise for my call :

function get(configKey) {
const config = CONFIGS[configKey];

return new Promise((resolve, reject) => {
    superagent.get(`//${document.location.host}${config.url}`) // eslint-disable-line no-undef
        .end((err, response) => {
            if (err) {
                displayErrorMessage(reject, config.error, err);
            } else {
                resolve(response.body);
            }
        });
});
}

and this unit test for it (specifically to detect the success):

 it("should detect success.", () => {
        const expected = {
            foo: 'bar'
        };

        testHelpers.nock(testHelpers.TEST_HOST)
            .get(config.url)
            .reply(200, { foo: 'bar' });

        return services.loadCollections()
            .then(
                testHelpers.expectedResolvedDeep.bind(null, expected),
                testHelpers.unexpectedRejected
            );
    }); 

All I did was add the // infront of the url , specifically in superagent.get(//${document.location.host}${config.url}) , because this app is being run in an iframe in another app. I have triple checked all the urls being passed, and this test was working fine before I added the // infront to get the same protocol so the call won't yell about a cross origin in an iframe. I am stumped as to why this now makes my tests give me an ECONNREFUSED error back, when they were working.

I have been stuck on this for hours now, and would be grateful for any advice! Thank you for taking the time to read.

Just for reference, the testhelpers.TEST_HOST is referring to this bit of code :

const TEST_HOST = 'http://fake.host';

// This is needed in order to simulate to be in a browser environment.
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.document = doc;
global.document.location.href = TEST_HOST;
global.window = doc.defaultView;

Aucun commentaire:

Enregistrer un commentaire