首页 > 解决方案 > 套接字在开玩笑路由器测试中挂断

问题描述

我在玩笑测试中遇到问题,出现“套接字挂断”错误,如下所示:

Error: socket hang up

    at connResetException (internal/errors.js:613:14)
    at Socket.socketOnEnd (_http_client.js:485:23)
    at Socket.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1218:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

它是随机发生的,有时我的测试正常,有时却不行。

我遵循了一些开玩笑测试的要求(尝试使用 return 而不是 async/await 等,但似乎没有任何效果,我做错了什么吗?

这是导致问题的代码:

import {of, throwError} from 'rxjs';
import { Server } from 'http';
import request from 'supertest';

import { ServerRoutes } from '~/common/routes';
import EcoDesignService from '~/server/domain/ecodesign/ecodesign.service';
import { mockAuthentication, validToken } from '~/server/test-utils';
import serverApp from '~/server';
import { Locale } from '~/client/services/translations';
import {
  addedDemarcheExemple,
  demarcheExemple,
  modelExemple,
  productExemple,
} from '~/common/entities/product';
import { frenchActionsExemple } from '~/common/entities/action';
import {sample} from "~/common/test-utils";
import {anyAxiosError} from "~/common/models/arbitrary";
import * as httpHelper from "~/server/utils/http/http.helper";

let server: Server;

describe('ecodesign router', () => {
  mockAuthentication();
  const getProductMock = jest
    .spyOn(EcoDesignService.prototype, 'getProduct')
    .mockReturnValue(of(productExemple));
  const getActionsMock = jest
    .spyOn(EcoDesignService.prototype, 'getActions')
    .mockReturnValue(of(frenchActionsExemple));
  const updateModelEcoDesignedMock = jest
    .spyOn(EcoDesignService.prototype, 'updateModelEcoDesigned')
    .mockReturnValue(of(void 0));
  const addDemarcheMock = jest
    .spyOn(EcoDesignService.prototype, 'addDemarche')
    .mockReturnValue(of(addedDemarcheExemple));
  const deleteDemarcheMock = jest
    .spyOn(EcoDesignService.prototype, 'deleteDemarche')
    .mockReturnValue(of(void 0));
  const updateDemarcheMock = jest
    .spyOn(EcoDesignService.prototype, 'updateDemarche')
    .mockReturnValue(of(addedDemarcheExemple));
  const handleEcoDesignAxiosErrorMock = jest
    .spyOn(httpHelper, 'handleEcoDesignAxiosError');

  beforeEach(() => {
    return server = serverApp.listen();
  });

  afterEach(() => {
    jest.clearAllMocks();
    return server.close();
  });

describe('/products', () => {
    it('should return a 200 with the corresponding product', async () => {
      const code = 11233;
      const locale = Locale.FR;

      const response = await request(serverApp)
        .get(`${ServerRoutes.ECO_DESIGN_PRODUCT}?conceptionOrModelCode=${code}&locale=${locale}`)
        .expect(() => {
          expect(getProductMock).toHaveBeenCalledTimes(1);
          expect(getProductMock).toHaveBeenCalledWith(code, locale, validToken);
        });
      expect(response.status).toBe(200);
    });

    it('should not return the product but return an error by handleEcoDesignAxiosError', async () => {
      const code = 11233;
      const locale = Locale.FR;
      const axiosError = sample(anyAxiosError);

      jest
        .spyOn(EcoDesignService.prototype, 'getProduct')
        .mockReturnValue(throwError(() => axiosError));

      const response = await request(serverApp)
        .get(`${ServerRoutes.ECO_DESIGN_PRODUCT}?conceptionOrModelCode=${code}&locale=${locale}`)
        .expect(() => {
          expect(handleEcoDesignAxiosErrorMock).toHaveBeenCalledTimes(1);
        });
      expect(response.status).toBe(axiosError.response.status);
    });
  });

  describe('/actions', () => {
    it('should return a 200 with given actions', async (done) => {
      const locale = Locale.FR;

      const response = await request(serverApp)
        .get(`${ServerRoutes.ECO_DESIGN_ACTIONS}?locale=${locale}`)
        .expect(() => {
          expect(getActionsMock).toHaveBeenCalledTimes(1);
          expect(getActionsMock).toHaveBeenCalledWith(locale, validToken);
        });
      expect(response.status).toBe(200);
      done();
    });

    it('should not return the actions but return an error by calling handleEcoDesignAxiosError', async () => {
      const locale = Locale.FR;
      const axiosError = sample(anyAxiosError);

      jest
        .spyOn(EcoDesignService.prototype, 'getActions')
        .mockReturnValue(throwError(() => axiosError));

     const response = await request(serverApp)
        .get(`${ServerRoutes.ECO_DESIGN_ACTIONS}?locale=${locale}`)
        .expect(() => {
          expect(handleEcoDesignAxiosErrorMock).toHaveBeenCalledTimes(1);
        });
     expect(response.status).toBe(axiosError.response.status);
    });
  });

它总是导致套接字在每个描述的第二次测试中挂断!感谢帮助 !

标签: reactjsaxiosrequestts-jest

解决方案


推荐阅读