Testing binary response with supertest
Asked Answered
F

1

7

I'm developing an API with express and testing it with supertest. My API endpoint is returning tar.gz file. I would like to test, if file is properly sent and it's content is correct. I'm having troubles figuring out how to retrieve data. My naive approach was to save content of res.text (where const res = request(app).get('/project/export') to a file, extract it and check it's content. But simple saving of res.text does not seem to work and extracting function does not recognise it as properly compressed file.

Any help appreciated. Feel free to suggest other modules/approaches how to test an express app. Thanks!

Flog answered 3/1, 2020 at 10:7 Comment(0)
J
7

When running tests in Jest, setting .responseType('blob') on the request will cause response.body to be a Buffer.

https://visionmedia.github.io/superagent/#binary

For example:

const response = await request(app)
  .get('/project/export')
  .responseType('blob')

await fs.promises.writeFile('export.tar.gz', response.body)
Justicz answered 9/6, 2020 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.