[Tips] How to test downloaded Excel file from response #2777

Open
opened 2022-08-29 08:20:51 +00:00 by pleymor · 2 comments
pleymor commented 2022-08-29 08:20:51 +00:00 (Migrated from github.com)

Gist to test the content of an Excel File in a controller (e2e) test, with a lib like supertest:

  function binaryParser(res, callback) {
    res.setEncoding('binary')
    res.data = ''
    res.on('data', function (chunk) {
      res.data += chunk
    })
    res.on('end', function () {
      callback(null, new Buffer(res.data, 'binary'))
    })
  }

  describe('exports/:id (GET)', () => {
    it('should return an excel File', async () => {
      const res = await request(app.getHttpServer())
        .get('/exports/xxx')
        .expect('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8')
        .buffer()
        .parse(binaryParser)

      assert.ok(Buffer.isBuffer(res.body))

      const workbook = XLSX.read(res.body, { type: 'buffer' })
      const sheet = workbook.Sheets[workbook.SheetNames[0]]
      const json = XLSX.utils.sheet_to_json(sheet, { header: 1 })

      expect(json).toEqual([
        ['My first Row', 'My second Row'],
        ['val 1', 50],
        ['val 2', 10]
      ])
    })
  })
Gist to test the content of an Excel File in a controller (e2e) test, with a lib like supertest: ```ts function binaryParser(res, callback) { res.setEncoding('binary') res.data = '' res.on('data', function (chunk) { res.data += chunk }) res.on('end', function () { callback(null, new Buffer(res.data, 'binary')) }) } describe('exports/:id (GET)', () => { it('should return an excel File', async () => { const res = await request(app.getHttpServer()) .get('/exports/xxx') .expect('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8') .buffer() .parse(binaryParser) assert.ok(Buffer.isBuffer(res.body)) const workbook = XLSX.read(res.body, { type: 'buffer' }) const sheet = workbook.Sheets[workbook.SheetNames[0]] const json = XLSX.utils.sheet_to_json(sheet, { header: 1 }) expect(json).toEqual([ ['My first Row', 'My second Row'], ['val 1', 50], ['val 2', 10] ]) }) }) ```
SheetJSDev commented 2022-08-29 08:31:59 +00:00 (Migrated from github.com)

Very cool, thanks for sharing! Server demos like the express example currently recommend opening the page with a browser (or using curl to upload data manually)

Is there a reason why the content type is text/xlsx rather than the typical application/vnd.ms-excel or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ?

Very cool, thanks for sharing! [Server demos like the `express` example](https://docs.sheetjs.com/docs/demos/server#express) currently recommend opening the page with a browser (or using curl to upload data manually) Is there a reason why the content type is `text/xlsx` rather than the typical `application/vnd.ms-excel` or `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` ?
pleymor commented 2022-08-29 08:37:46 +00:00 (Migrated from github.com)

The content type is a mistake of mine, I fix it right now. Thank you 🙏

The content type is a mistake of mine, I fix it right now. Thank you 🙏
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2777
No description provided.