I have a problem in my jest tests, I have a "socket hang up" error like the following :
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)
It happens randomly, sometimes my tests are ok and sometime they are not.
I followed some requirements for jest testing (tried use return instead of async/await etc. but nothing seems to work, did I do something wrong ?
Here is the code that causes problem :
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);
});
});
It always cause socket hang up in the second test of each describe ! Thanks for help !