在Heroku上运行supertest-fetch的测试

我们想在每次发布之前在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);
    });
});

Procfile:

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(这可能在你的FetchFunction或makeFectch文件中配置),这在你的Heroku环境中不起作用.如果您知道要测试的服务的公开公开的dns目标,则可以将其添加到您的配置中.或者,如果你想要更有活力,你可以使用
the dyno metadata来组合一个有效的公共网址.

点赞