使用 multer、typescript、mongo 和 supertest 测试上传 api

问题描述 投票:0回答:0

我正在尝试制作一个允许使用 multer、nodejs (typescript) 和 supertest 上传文件的 REST api。

我在文件上传部分遇到了麻烦,因为 supertest 抛出了一个超时异常和这个错误。

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TCPSERVERWRAP

      44 |         console.log("GO TEST!!!!!!!!!!!!!")
      45 |         const response = await request(app)
    > 46 |         .post('/api/v1.0.0/clients/'+default_client._id+'/uploads')
         |          ^
      47 |         .set('x-api-key', CONSTANT.PRIVATE_API_KEY)
      48 |         .attach('file', MOCKFOLDER + IMAGES[0]);
      49 |

      at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
      at new Test (node_modules/supertest/lib/test.js:34:14)
      at Object.obj.<computed> [as post] (node_modules/supertest/index.js:43:18)
      at src/__tests__/uploads-api.test.ts:46:10
      at src/__tests__/uploads-api.test.ts:8:71
      at Object.<anonymous>.__awaiter (src/__tests__/uploads-api.test.ts:4:12)
      at Object.<anonymous> (src/__tests__/uploads-api.test.ts:43:106)


  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "errorHandler receive /api/v1.0.0/clients/64162afa3b284c6903193fba/uploads MongoNotConnectedError: Client must be connected before running operations

我用邮递员对其进行了测试,效果非常好(甚至不需要 2 秒),但无法进行此测试。特别是在某一时刻,我收到一个错误,指出 mongo 未连接,但如果是,它甚至会添加一个新客户端......其余与文件上传无关的测试工作完美

这就是我所拥有的:

beforeAll(async () => {
    await dbConnect();
    IMAGES = fs.readdirSync(MOCKFOLDER);
    default_client = await createClient();
});
afterAll(async () => {
    await dbClose();
});

export async function createClient(options?: {[k:string]: any}){
    const responsePromise = request(app)
    .post('/api/v1.0.0/clients')
    .set('x-api-key',  CONSTANT.PRIVATE_API_KEY);
    if(options){
        responsePromise.send(options);
    }
    responsePromise.expect(200)
    .expect(function(res) {
        assert(res.body.hasOwnProperty('client'));
        assert(res.body.client.hasOwnProperty('_id'));
    });
    const response = await responsePromise;
    return response.body.client;
}

describe("Pruebas relacionadas con la subida de archivos", ()=>{

     test("Permite subir imagen con formato correcto, devuelve identificador, envia id_client", async ()=>{
        console.log("GO TEST!!!!!!!!!!!!!")
        const response = await request(app)
        .post('/api/v1.0.0/clients/'+default_client._id+'/uploads')
        .set('x-api-key', CONSTANT.BEHIS_PRIVATE_API_KEY)
        .attach('file', MOCKFOLDER + IMAGES[0]);
        console.log("YYYYYYYYYYYYYYYYYYYY", response.body)
        expect(response.status).toBe(200);
    });

在服务器上,我使用了几个“中间件”来保护数据,但它们运行良好,我在其余测试中使用它们:

router.post("/clients/:id_client/uploads", checkPrivateKeyNext, checkIdClientNext, checkFolderClient, (req: CustomRequest, res, next)=>{req.locals = res.locals; next()}, uploadMulter.single("file"), addUpload);

有了日志,它到达了 multer 处理程序,但后来它不再到达 addUpload 函数

当我看到 mongo 错误时,我试图更改通过日志获取更多信息的承诺,这很奇怪,因为它连接正确。此外,文件上传正确,甚至从 db 返回引用的客户端...

有什么想法吗?问候

node.js typescript multer supertest
© www.soinside.com 2019 - 2024. All rights reserved.