javascript - Unit Testing Controllers use Jest, NodeJS - Stack Overflow

I want to check a case that certain routes are calling the correct controller use Jest specific (mock o

I want to check a case that certain routes are calling the correct controller use Jest specific (mock or spy).

It is case specific for unit testing. Somebody can help me how to check it use jest. I don't need verify kind of expect (status code or res object) i need to check if controller have been called. Thanks!

For instance:

// todoController.js
function todoController (req, res) {
    res.send('Hello i am todo controller')
} 

// index.spec.js
const express = require('express');
const request = require('request-promise');
const todoController = require('./todoController');
jest.mock('./todoController');

const app = express();

app.get('/todo', todoController)

test('If certain routes are calling the correct controller , controller should to have been called times one.', async() => {
    await request({url: 'http://127.0.0.1/todo'})
    expect(todoController).toHaveBeenCalledTimes(1);
})

I want to check a case that certain routes are calling the correct controller use Jest specific (mock or spy).

It is case specific for unit testing. Somebody can help me how to check it use jest. I don't need verify kind of expect (status code or res object) i need to check if controller have been called. Thanks!

For instance:

// todoController.js
function todoController (req, res) {
    res.send('Hello i am todo controller')
} 

// index.spec.js
const express = require('express');
const request = require('request-promise');
const todoController = require('./todoController');
jest.mock('./todoController');

const app = express();

app.get('/todo', todoController)

test('If certain routes are calling the correct controller , controller should to have been called times one.', async() => {
    await request({url: 'http://127.0.0.1/todo'})
    expect(todoController).toHaveBeenCalledTimes(1);
})
Share Improve this question edited Apr 3, 2019 at 17:47 brass monkey 6,77110 gold badges43 silver badges66 bronze badges asked Apr 1, 2019 at 21:33 Roman StefankoRoman Stefanko 3952 gold badges3 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 24

Actually if you search, there are many references out there.

In the following, I share a few ways that I know.

One of the big conceptual leaps to testing Express applications with mocked request/response is understanding how to mock a chained

API eg. res.status(200).json({ foo: 'bar' }).

First you can make some kind of interceptor, this is achieved by returning the res instance from each of its methods:

// util/interceptor.js
module.exports = {
  mockRequest: () => {
    const req = {}
    req.body = jest.fn().mockReturnValue(req)
    req.params = jest.fn().mockReturnValue(req)
    return req
  },

  mockResponse: () => {
    const res = {}
    res.send = jest.fn().mockReturnValue(res)
    res.status = jest.fn().mockReturnValue(res)
    res.json = jest.fn().mockReturnValue(res)
    return res
  },
  // mockNext: () => jest.fn()
}

The Express user-land API is based around middleware. AN middleware that takes a request (usually called req), a response (usually called res ) and a next (call next middleware) as parameters.

And then you have controller like this :

// todoController.js
function todoController (req, res) {
    if (!req.params.id) {
      return res.status(404).json({ message: 'Not Found' });
    }

    res.send('Hello i am todo controller')
}

They are consumed by being “mounted” on an Express application (app) instance (in app.js):

// app.js
const express = require('express');
const app = express();

const todoController = require('./todoController');

app.get('/todo', todoController);

Using the mockRequest and mockResponse we’ve defined before, then we’ll asume that res.send() is called with the right payload ({ data }).

So on your test file :

// todo.spec.js
const { mockRequest, mockResponse } = require('util/interceptor')
const controller = require('todoController.js')

describe("Check method \'todoController\' ", () => {
  test('should 200 and return correct value', async () => {
    let req = mockRequest();
    req.params.id = 1;
    const res = mockResponse();

    await controller.todoController(req, res);

    expect(res.send).toHaveBeenCalledTimes(1)
    expect(res.send.mock.calls.length).toBe(1);
    expect(res.send).toHaveBeenCalledWith('Hello i am todo controller');
  });

  test('should 404 and return correct value', async () => {
    let req = mockRequest();
    req.params.id = null;
    const res = mockResponse();

    await controller.todoController(req, res);

    expect(res.status).toHaveBeenCalledWith(404);
    expect(res.json).toHaveBeenCalledWith({ message: 'Not Found' });
  });
});

This is only 1 approach to testing Express handlers and middleware. The alternative is to fire up the Express server.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1738521706a4054369.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信