我正在尝试使用jestJS为此功能编写一个测试,但是在模拟socket.emit()
和getPwmDutyCycle()
函数时遇到了一些问题:
module.exports = async (app, socket) => {
const res = []
const array = ['some', 'example', 'elements']
array.map((elm, index) => {
res.push(app.locals['target' + (index + 1)].getPwmDutyCycle())
})
socket.emit('status', res)
}
这是我想出的:
const status = require('../lib/status.js')
test('should emit dc values', () => {
const app = {
locals: {
target1: 1,
target2: 2,
target3: 3
}
}
const socket = { emit: jest.fn() }
status(app, socket)
expect(socket.emit).toHaveBeenCalled()
expect(socket.emit.mock.calls[0][0]).toBe('status')
expect(socket.emit.mock.calls[1][0]).toBe([1, 2, 3])
})
答案 0 :(得分:0)
您的app
存根未使用getPwmDutyCycle
方法:
const app = {
locals: {
target1: { getPwmDutyCycle: () => 1},
target2: { getPwmDutyCycle: () => 2},
target3: { getPwmDutyCycle: () => 3}
}
}