javascript - 表达JS/jestJS : How to split get() function to write simple jest unit test?

标签 javascript unit-testing express jestjs

如何在 expressJS 应用程序中定义 get() 路由以进行简单的单元测试?

因此,作为第一步,我将 get() 的函数移到了自己的文件中:

index.js

const express = require('express')
const socketIo = require('socket.io')
const Gpio = require('pigpio').Gpio

const app = express()
const server = http.createServer(app)
const io = socketIo(server)

const setStatus = require('./lib/setStatus.js')

app.locals['target1'] = new Gpio(1, { mode: Gpio.OUTPUT })

app.get('/set-status', setStatus(app, io))

lib/setStatus.js

const getStatus = require('./getStatus.js')

module.exports = (app, io) => {
  return (req, res) => {
    const { id, value } = req.query // id is in this example '1'
    req.app.locals['target' + id].pwmWrite(value))
    getStatus(app, io)
    res.send({ value }) // don't need this
  }
}

lib/getStatus.js

const pins = require('../config.js').pins

module.exports = async (app, socket) => {
  const res = []
  pins.map((p, index) => {
    res.push(app.locals['target' + (index + 1)].getPwmDutyCycle())
  })
  socket.emit('gpioStatus', res)
}

所以首先我不太确定,如果我正确地拆分代码 - 考虑进行单元测试。

对我来说,唯一必须通过调用 /set-status?id=1&value=50 来调用 pwmWrite()(我猜)对象,由new Gpio定义,保存在expressJS的locals中。

第二个:如果这应该是正确的方法,我不明白如何编写 jestJS 单元测试来检查 pwmWrite 是否已被调用 - 这是在异步函数内部。

这是我的尝试,但我无法测试 pwmWrite 的内部调用:

test('should call pwmWrite() and getStatus()', async () => {
  const app = {}
  const io = { emit: jest.fn() }
  const req = {
    app: {
      locals: {
        target1: { pwmWrite: jest.fn() }
        }
      }
    }
  }
  expect.assertions(1)
  expect(req.app.locals.target1.pwmWrite).toHaveBeenCalled()
  await expect(getStatus(app, io)).toHaveBeenCalled()
})

最佳答案

您非常接近,只是缺少一些东西。

您需要在 expect 语句之前调用方法 setStatusgetStatus, 并且您缺少对 req.queryres 的模拟,因为 getStatus 使用它们。

test('should call pwmWrite() and getStatus()', async () => {
  const app = {}
  const io = {};
  const req = {
    query: {
      id: '1',
      name: 'foo'
    },
    app: {
      locals: {
          target1: { pwmWrite: jest.fn() }
      }
    }
  };
  const res = { send: jest.fn() };

  // Mock getStatus BEFORE requiring setStatus
  jest.mock('./getStatus');

  //OBS Use your correct module paths
  const setStatus = require('./setStatus');
  const getStatus = require('./getStatus');


  // Call methods
  setStatus(app, io)(req, res);

  expect.assertions(2);

  // Get called in setStatus
  expect(req.app.locals.target1.pwmWrite).toHaveBeenCalled();

  // See if mocked getStatus has been called
  await expect(getStatus).toHaveBeenCalled();
});

getStatus 需要在要求 setStatus 之前被模拟,因为它在那里使用

关于javascript - 表达JS/jestJS : How to split get() function to write simple jest unit test?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52842582/

相关文章:

javascript - 读取输入并用 php 回显其他内容

c# - Visual Studio C# 单元测试

javascript - 在继续之前,我如何确保我的 postgres promise 得到解决?

angularjs - 模拟过滤器中使用的服务

javascript - Node JS Express 和控制台输出到命令行与浏览器

node.js - 每个 axios 请求在 React-Redux 应用程序中都会触发两次?

javascript - 为什么这个日期比较在 JavaScript 中不起作用?

javascript - 即使 java 脚本应该启用它,文本框仍然处于禁用状态

JavaScript : How to do Error Handling in Lexer generated by antlr?

python - 如何在 python 的单元测试中使用 assertRaises() 来捕获语法错误?