Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock an axios request using sinon modules

There seems to be so many different ways to do this, but I am trying to use just sinon, sinon-test, chai/mocha, axios, httpmock modules. I am not able to successfully mock a GET call made using axios. I want to be able to mock the response from that axios call so the unit test won't actually have to make the external API request.

I've tried setting up a basic unit test by creating a sandbox, and using sinon stub to set up a GET call and specify the expected response. I'm unfamiliar with JavaScript and NodeJS.

// Main class (filename: info.js)

function GetInfo(req, res) {
    axios.get(<url>).then(z => res.send(z.data));
}

// Test class (filename: info.test.js)

it ("should return info", () => {
    const expectedResponse = "hello!";
    const res = sinon.spy();
    const aStub = sinon.stub(axios, "get").resolves(Promise.resolve(expectedResponse));

    const req = httpMock.createRequest({method:"get", url:"/GetInfo"});

    info.GetInfo(req, res);

    // At this point, I need to evaluate the response received (which should be expectedResponse)
    assert(res.data, expectedResponse); // data is undefined, res.status is also undefined

    // How do I read the response received?

});

I need to know how to read the response that is supposed to be sent back (if it is being captured in the first place by sinon).

like image 753
rick200 Avatar asked Jul 04 '19 05:07

rick200


People also ask

How do you mock this in Sinon?

Mocks allow you to create a fake function that passes or fails depending on your needs. You can ensure it was called with certain arguments, or check how many times it was called. You must call mock() on an object.

How does Sinon JS work?

Sinon splits test-doubles into three types: Spies, which offer information about function calls, without affecting their behavior. Stubs, which are like spies, but completely replace the function. This makes it possible to make a stubbed function do whatever you like — throw an exception, return a specific value, etc.

What is Sinon in JavaScript?

Sinon JS is a popular JavaScript library that lets you replace complicated parts of your code that are hard to test for “placeholders,” so you can keep your unit tests fast and deterministic, as they should be.


1 Answers

I'm assuming the response you're wanting to check is the z.data being passed to res.send(z.data)

I don't think your Sinon Spy is being set up correctly.

In your example, res is a function created by sinon. This function won't have a property data.

You probably want to create a spy like this:

const res = {
  send: sinon.spy()
}

This gives you a res object which has a spy with the key send. You can then make assertions about the parameters used to call res.send

it ("should return info", () => {
    const expectedResponse = "hello!";
    const res = {
      send: sinon.spy()
    };
    const aStub = sinon.stub(axios, "get").resolves(Promise.resolve(expectedResponse));

    const req = httpMock.createRequest({method:"get", url:"/GetInfo"});

    info.GetInfo(req, res);

    // At this point, I need to evaluate the response received (which should be expectedResponse)
    assert(res.send.calledWith(expectedResponse)); // data is undefined, res.status is also undefined

});
like image 75
Nick Nelson Avatar answered Oct 01 '22 11:10

Nick Nelson