使用 Jest 进行单元测试 express-http-proxy

发布于 2025-01-16 19:38:55 字数 1021 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文