簡體   English   中英

響應為 HTTP 400 時 SuperTest 超時

[英]SuperTest times out when response is HTTP 400

在使用 SuperTest 和 Mocha 創建一些測試時:

import supertest from 'supertest';
import { should } from 'chai';
import app from '../app.mjs';

const request = supertest(app);
should();

describe('HTTP GET', () => {
    describe('/api/v1/songs', () => {
        it('When request has no parameters, then response Status-Code should be 200 (OK)', async () => {
            const response = await request.get('/api/v1/songs');
            response.statusCode.should.equal(200);
        });
    });
    describe('/api/v1/songs/year/:year', () => {
        it('When parameter is an unknown year, then response Status-Code should be 400 (Bad Request)', async () => {
            const response = await request.get('/api/v1/songs/year/1776');
            response.statusCode.should.equal(400);
        });
        it('When parameter is an valid year, then response Status-Code should be 200 (OK)', async () => {
            const response = await request.get('/api/v1/songs/year/1977');
            response.statusCode.should.equal(200);
        });
        it('When parameter is an valid year, then response should include songs matching that year', async () => {
            const response = await request.get('/api/v1/songs/year/1977');
            response.body.forEach((song) => {
                song.should.have.property('year', 1977);
            });
        });
    });
});

我注意到奇怪的是那些超時的是那些返回HTTP 400的:

HTTP GET
/api/v1/songs
  ✔ When request has no parameters, then response Status-Code should be 200 (OK)
/api/v1/songs/year/:year
  1) When parameter is an unknown year, then response Status-Code should be 400 (Bad Request)
  ✔ When parameter is an valid year, then response Status-Code should be 200 (OK)
  ✔ When parameter is an valid year, then response should include songs matching that year

錯誤消息不會添加太多上下文:

  1) HTTP GET
       /api/v1/songs/year/:year
         When parameter is an unknown year, then response Status-Code should be 400 (Bad Request):
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我不確定是否需要做其他事情(例如 try/catch)才能使其正常工作?

我已經能夠解決這個問題:原來 controller 操作沒有在res.status(400)之后調用.end() ) 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM