【发布时间】:2019-11-06 15:19:00
【问题描述】:
我在index.js 中设置了如下的快速 POST 路由
import * as Busboy from 'busboy';
public publish = async (req: Request, res: Response) => {
const busboy = new Busboy({ headers: req.headers });
const pl = { title: '' };
busboy.on('field', (fieldname, val) => {
switch (fieldname) {
case 'title':
pl.title = val;
break;
}
});
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
// Process files
});
busboy.on('finish', async () => {
// Process request
res.send({payload: pl});
});
}
在测试index.test.js 使用玩笑我如何模拟这个模块,以便我可以验证包含表单字段title 在请求中发送的响应?
目前我使用jest.mock('busboy');,但没有任何东西因此被调用。
jest.mock('busboy');
let service: ServiceController;
describe('Mosaic Research Capture Service', () => {
it('should publish', async () => {
service = new ServiceController();
const req = {
headers: {
'Content-Type': 'multipart/form-data'
},
body: {}
};
const res = {
send: jest.fn()
};
await service.publish(req, res);
});
});
React 客户端调用这个请求如下
const formData = new FormData();
formData.append('title', 'SomeTitle');
const header = {
credentials: 'include',
'Content-Type': 'multipart/form-data',
};
const response = await axios.post('/publish, formData, header);
【问题讨论】:
标签: javascript node.js jestjs busboy