How to test response data from Express in Jest

Found a fix! Leaving this here for someone else who might struggle with the same.

When returning data using res.send(), res.json() or something similar, the response object (from const response = httpMocks.createResponse();) itself is updated. The data can be collected using res._getData():

const httpMocks = require('node-mocks-http');
const { sendSomeStuff } = require('/some/path/to/middleware');

describe('sendSomeStuff', () => {
    test('should send some stuff', () => {
        const request = httpMocks.createRequest({
            method: 'GET',
            url: '/some/url'
        });

        const response = httpMocks.createResponse();

        sendSomeStuff(request, response, (err) => {
            expect(err).toBeFalsy();
        });

        const { property } = JSON.parse(response._getData());

        expect(property).toBe('someValue');
        });
    });
});

I did a different way by utilising jest.fn(). For example: if you wanna test res.json({ status: YOUR_RETURNED_STATUS }).status(200);

const res = {};
res.json = jest.fn(resObj => ({
    status: jest.fn(status => ({ res: { ...resObj, statusCode: status } 
  })),
}));

Basically, I mock the res chain methods(json and status).

That way you can do expect(YOUR_TEST_FUNCTION_CALL).toEqual({ res: { status: 'successful', statusCode: 200 }}); if your response structure is like that.