使用 Jest 进行单元测试 express-http-proxy
我有一个 custom-proxy.ts
模块,它导出如下函数:
import proxy from 'express-http-proxy';
export const customProxy = proxy('a-url', {
filter: 'POST',
https: true
}
这是使用快速路由注册的,如下所示:
server.all('/some/route', customProxy);
我想测试 proxy
是否被调用使用 Jest 的自定义参数。
我尝试使用以下方法对其进行测试:
import proxy from 'express-http-proxy';
import * as CustomProxy from 'custom-proxy';
import request from 'supertest';
describe('test', () => {
customProxySpy = jest.spyOn(CustomProxy, 'customProxy');
beforeEach(() => {
server = express;
server.all('/some/route', customProxy);
});
it('should pass', async () => {
await request(server).get('/some/route').send();
expect(customProxySpy).toHaveBeenCalledWith('a-url', {
filter: 'POST',
https: true
});
});
});
但是,使用 IncomingMessage
和一些标头调用 customProxySpy
。
有谁知道如何验证在 proxy() 中调用的自定义参数?
I have a custom-proxy.ts
module that exports a function like this:
import proxy from 'express-http-proxy';
export const customProxy = proxy('a-url', {
filter: 'POST',
https: true
}
This is registered with an express route as follows:
server.all('/some/route', customProxy);
I would like to test that proxy
is called with the custom arguments using Jest.
I have tried to test it with the following approach:
import proxy from 'express-http-proxy';
import * as CustomProxy from 'custom-proxy';
import request from 'supertest';
describe('test', () => {
customProxySpy = jest.spyOn(CustomProxy, 'customProxy');
beforeEach(() => {
server = express;
server.all('/some/route', customProxy);
});
it('should pass', async () => {
await request(server).get('/some/route').send();
expect(customProxySpy).toHaveBeenCalledWith('a-url', {
filter: 'POST',
https: true
});
});
});
However the customProxySpy
is getting called with IncomingMessage
and some headers.
Does anyone know how I can verify the custom arguments get called in proxy()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论