0

I am trying to get my unit tests to pass/fail based on their response code. I am trying to pass a variable statusCode to equal the result of the function callback but the value is only temporary since it is enclosed within the function. I have tried letting statusCode = callback() but I need to pass in the parameters of the function. I don't know what other steps to take, any help would be greatly appreciated.

let statusCode;
it('should be able to get all certificates', async () => {
        const request = require('request');

        const headers = {
            accept: 'application/json',
            Authorization: ('Bearer ' + Authorization),
            signature: Signature,
        };

        const options = {
            url: url + '/v3/certificates',
            headers: headers,
        };

        function callback(error, response, body) {
            if (!error) {
                console.log("Certificates: " + body);
            }
            statusCode = response.statusCode;
            console.log("Code : " + statusCode);
            return  response.statusCode;
        }

        request(options, callback);
        console.log("Code 2 : " + statusCode);
        console.debug(assert.equal(statusCode, 200));
    });
  • 1
    You seem to be making a real request; that's not a unit test. Also you have an async test but don't await anything, and you should probably read https://stackoverflow.com/q/14220321/3001761. – jonrsharpe Apr 06 '20 at 16:15
  • I am testing the endpoints automatically as opposed to through swagger, any insight on how I can assert that I am getting the right or wrong status code to make the test fail/pass? – Jeremiah O Sullivan Apr 06 '20 at 16:22
  • I see, this is what you'd usually call an *integration* test, then. Given that [`request` is deprecated](https://www.npmjs.com/package/request) I'd recommend picking another request library, ideally one supporting promises, and read their docs to see how to make a request and get its status code. – jonrsharpe Apr 06 '20 at 16:23
  • Thanks man, I will take a look at it. – Jeremiah O Sullivan Apr 07 '20 at 11:34

1 Answers1

0

I just needed to wait for the status code to resolve and that solved my problem. By just making the program wait for 5 seconds, it gives time for your code to resolve.

let statusCode;
it('should be able to get all certificates', async () => {
        const request = require('request');

        const headers = {
            accept: 'application/json',
            Authorization: ('Bearer ' + Authorization),
            signature: Signature,
        };

        const options = {
            url: url + '/v3/certificates',
            headers: headers,
        };

        function callback(error, response, body) {
            if (!error) {
                console.log("Certificates: " + body);
            }
            statusCode = response.statusCode;
            console.log("Code : " + statusCode);
            return  response.statusCode;
        }

        request(options, callback);
        sleep(5000);
        console.log("Code 2 : " + statusCode);
        console.debug(assert.equal(statusCode, 200));
    });```