0

In the following helloWorld.test.ts I can't get access to res.text to test the HTML result:

import * as chai from 'chai';
import chaiHttp = require('chai-http');

import app from '../src/App';

chai.use(chaiHttp);
const expect = chai.expect;

describe('baseRoute', () => {

  it('should be html', async () => {
    const res = await chai.request(app).get('/');
    expect(res.type).to.eql('text/html');
  });

  it('should have the message in body', async () => {
    const res = await chai.request(app).get('/');
    //console.log("Result:", res);
    expect(res.text).to.eql('<html><head><title>Hey</title></head><body><h1>Hello world!</h1></body></html>');
  });

});

When I try to build my project in VSCode, I get a Property 'text' does not exist on type 'Response'. error, which is consistent with the autocompletion that only shows three properties for a Response: body, status, and type.

The res.body is always {} so, according to the answer to the following question, one should use res.text (in JavaScript): Chai response.body is always empty {}

How to get access in TypeScript to res.text?

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111

1 Answers1

1

In my case, I had @types/chai-http installed (from an old tutorial), but https://www.npmjs.com/package/@types/chai-http says it was deprecated. I'm assuming that in TypeScript it was a very limited API, as the version was 0.0.23 (or something).

npm uninstall @types/chai-http opened up the autocompletion for all the properties, including .text. This solved my problem.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111