gpt4 book ai didi

heroku - 在 Heroku 上运行 supertest-fetch 的测试

转载 作者:行者123 更新时间:2023-12-02 06:55:04 24 4
gpt4 key购买 nike

我们希望在每次发布之前在 Heroku 上运行 supertest-fetch 的测试。

示例测试:

import {FetchFunction, makeFetch} from 'supertest-fetch';
import {koa} from '../koa';
import {Server} from 'http';

describe('User controller', () => {

let fetch: FetchFunction;
let server: Server;
beforeAll(() => {
server = koa.listen(3101);
fetch = makeFetch(server);
});

afterAll(() => {
server.close();
});

it('GET /users should return 200', async () => {
const response = await fetch('/users');
expect(response.status).toEqual(200);
});
});

过程文件:

release: npm test && node db-tools/apply-migrations.js

在 Heroku 上运行该测试失败,并显示

remote: FAIL src/controller/user.spec.ts (5.506s)        
remote: User controller
remote: ✕ GET /users should return 200 (48ms)
remote:
remote: ● User controller › GET /users should return 200
remote:
remote: FetchError: request to https://localhost/users failed, reason: connect ECONNREFUSED 127.0.0.1:443
remote:
remote: at ClientRequest.<anonymous> (node_modules/node-fetch/lib/index.js:1345:11)
remote:

我不知道为什么 supertest-fetch 发出 https 请求,即使它发出 http 请求,我也不认为 Heroku 允许提供任意端口服务。有人在 Heroku 上成功运行 supertest 的测试吗?

最佳答案

现在 super-fetch 正在向 localhost:443 发出请求(这可能是在您的 FetchFunctionmakeFectch 中配置的 文件),这在您的 Heroku 环境中不起作用。如果您知道要测试的服务的公开暴露的 dns 目标,则可以将其添加到您的配置中。或者,如果您想要更有活力,您可以使用 the dyno metadata组合一个有效的公共(public) URL。

关于heroku - 在 Heroku 上运行 supertest-fetch 的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53132585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com