#1815·superagent

ECONNRESET Error When Uploading File in Jest Test Using superagent

Author: nipunaramCreated Sep 4, 2024Updated Sep 4, 2024
LabelsBug

Describe the bug

I'm encountering an econnrest error when attempting to upload a file in my Jest test using superagent. The error occurs when the attach() method is called to attach the file to the request.

Node.js version: 20.17

OS version: Windows 11 Pro

Code to reproduce

		test('should throw not found error when the question does not allow any files', async () => {
			let question = getQuestion()
			question.allowFile = false
			question.isFileRequired = false

			const mpg = pg as unknown as jest.Mock
			mpg.mockReturnValueOnce(mock(question))

			const res1 = await request({
				rolesId: UserRole.ADMIN,
			})
				.post('/questionnairee/question/1/answer/file')
				.attach('file', '__mocks__/data/files/image_1.png')   //This is where the issue is being produced
				.expect(404)

			expect(ErrorResSchema.safeParse(res1.body).success).toBeTruthy()

			question = getQuestion()
			question.allowFile = false
			question.isFileRequired = true

			mpg.mockReturnValueOnce(mock(question))

			const res2 = await request({
				rolesId: UserRole.ADMIN,
			})
				.post('/questionnairee/question/1/answer/file')
				.attach('file', '__mocks__/data/files/image_1.png')
				.expect(404)

			expect(ErrorResSchema.safeParse(res2.body).success).toBeTruthy()
		})

Checklist

  • [v] I have searched through GitHub issues for similar issues.
  • [v] I have completely read through the README and documentation.
  • [v] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.