javascript - JestJS:如何测试嵌套(异步)函数中的调用函数
问题描述
我正在尝试为嵌套函数编写单元测试,如下所示:
myFunction.js
const anotherFunction = require('./anotherFunction.js')
module.exports = (app, io) => {
return (req, res) => {
const { id, value } = req.query
req.app.locals['target' + id].pwmWrite(value)
anotherFunction(app, io)
res.send({ value })
}
}
我想测试是否pwmWrite()
并anotherFunction()
已被调用。
return (req, res) => {}
但是由于导入的功能,我遇到了一些问题。
这是我的尝试,但不起作用:
myFunction.test.js
test('should call pwmWrite() and anotherFunction()', async () => {
const app = {}
const io = { emit: jest.fn() }
const req = {
app: {
locals: {
target1: { pwmWrite: () => 25 }
}
}
}
}
expect.assertions(1)
expect(req.app.locals.target1.pwmWrite).toHaveBeenCalled()
await expect(myFunction(app, io)).resolves.toEqual(25)
})
解决方案
这是解决方案:
myFunction.js
:
const anotherFunction = require('./anotherFunction.js');
module.exports = (app, io) => {
return (req, res) => {
const { id, value } = req.query;
req.app.locals['target' + id].pwmWrite(value);
anotherFunction(app, io);
res.send({ value });
};
};
anotherFunction.js
:
module.exports = (app, io) => {
return 'do something';
};
单元测试:
jest.mock('./anotherFunction');
const myFunction = require('./myFunction');
const anotherFunction = require('./anotherFunction');
describe('test suites', () => {
test('should call pwmWrite() and anotherFunction()', () => {
const app = {};
const io = { emit: jest.fn() };
const id = '1';
const value = 'jest';
const req = {
query: { id, value },
app: {
locals: {
target1: { pwmWrite: jest.fn() }
}
}
};
const res = { send: jest.fn() };
myFunction(app, io)(req, res);
expect(anotherFunction).toBeCalledWith(app, io);
expect(req.app.locals.target1.pwmWrite).toBeCalledWith(value);
});
});
带有覆盖率报告的单元测试结果:
PASS src/stackoverflow/52845000/myFunction.spec.js
test suites
✓ should call pwmWrite() and anotherFunction() (5ms)
--------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files | 88.89 | 100 | 66.67 | 88.89 | |
anotherFunction.js | 50 | 100 | 0 | 50 | 2 |
myFunction.js | 100 | 100 | 100 | 100 | |
--------------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.113s
这是完成的演示:https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/52845000
推荐阅读
- python - 如果列表在多个方向上有 4 个连续数字,则获取真假结果测试
- authentication - 为什么 Laravel 5 在验证用户身份后继续重定向到 /login(升级后)
- docker - 在 Jenkinsfile 中使用 Dockerfile 创建的 docker 容器内运行命令
- java - 第二次用字符串元素数组填充微调器时出错
- multithreading - pthread_create 使用 0 作为属性参数
- javascript - Javascript表单验证问题如何显示条件输入字段
- python - DeprecationWarning in Python 3.6 and 3.7 (with Pillow, distutils, imp)
- android - 为什么我在 Android 开发者中找不到“ACTION_REQUEST_SHUTDOWN”?
- java - Error when try to access multiple model attribute in thymeleaf
- ruby-on-rails - How do we get list of users within range of certain city?