最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Testing an express middleware with Jest - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Mar 17, 2020 at 13:41 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Mar 17, 2020 at 12:17 enbermudasenbermudas 1,6154 gold badges23 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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

发布评论

评论列表(0)

  1. 暂无评论