Skip to content

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

Description

@pleymor

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]
      ])
    })
  })

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions