enbermudas
enbermudas

Reputation: 1615

Testing an express middleware with Jest

I'm trying to test my controller (express middleware) with Jest. In order to explain my problem, I'll provide my code:

import request from 'utils/request';
import logger from 'config/logger';

const get = async (req, res, next) => {
  try {
    const response = await request.get('entries?content_type=category');
    return res.json(response.data);
  } catch (error) {
    logger.error(error.response.data.message);
    return next(error);
  }
};

module.exports = {
  get,
};

I need to test this get function. In order to do so, I need to provide the req, res and next args to it. I've found this question where the op talks about mocking the express request and then says "how to use it" but I can't see how. That's the only topic that I've seen so far directly related with my problem but the answer doesn't work for me (I don't want to use Nock nor any other library to adchieve the testing, just Jest).

So, how can I successfully test this function? Is it by using mocking or is there any other way?

Sorry for my bad english and thanks in advance for your help!

Upvotes: 4

Views: 15499

Answers (1)

Andrew Nolan
Andrew Nolan

Reputation: 2107

If you are writing unit tests, then mocking is the more appropriate way to go. using Jest, Mocking should be available out of the box. In a test file, it may look something like this:

import request from 'utils/request';
import logger from 'config/logger';
import { get } from 'middleware/get'; // Or whatever file you are testing

jest.mock('request'); // Mocking for a module import
jest.mock('logger'); // Mocking for a module import

const mockReq = () => {
  const req = {};
  // ...from here assign what properties you need on a req to test with
  return req;
};

const mockRes = () => {
    const res = {};
    res.status = jest.fn().mockReturnValue(res);
    res.json = jest.fn().mockReturnValue(res);
    return res;
};

test('should return a json response of data', () => {
    const mockedNext = jest.fn();
    const mockedReq = mockReq();
    const mockedRes = mockRes();
    const mockedEntries = {
        data: {}
    };/*...whatever mocked response you want back from your request*/
    request.get.mockResolvedValue(mockedEntries);

    const result = get(mockedReq, mockedRes, mockedNext);

    expect(result).to.equal(mockedEntires.data);
    expect(mockedNext.mock.calls.length).toBe(1);
    expect(mockedRest.json).toHaveBeenCalledWith(mockedRes.data)
});

Jest Mocking

Upvotes: 6

Related Questions