如何访问 TypeScript Chai 中的响应文本?
How to access Reponse text in TypeScript Chai?
在下面的 helloWorld.test.ts
中,我无法访问 res.text
来测试 HTML 结果:
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>');
});
});
当我尝试在 VSCode 中构建我的项目时,出现 Property 'text' does not exist on type 'Response'.
错误,这与仅显示 Response
的三个属性的自动完成一致:body
、status
和 type
。
res.body
总是 {}
所以,根据下面问题的答案,应该使用 res.text
(在 JavaScript 中):
如何在 TypeScript 中访问 res.text
?
在我的例子中,我安装了 @types/chai-http
(来自旧教程),但 https://www.npmjs.com/package/@types/chai-http 说它已被弃用。我假设在 TypeScript 中它是一个非常有限的 API,因为版本是 0.0.23(或其他)。
npm uninstall @types/chai-http
打开所有属性的自动完成,包括 .text
。这解决了我的问题。
在下面的 helloWorld.test.ts
中,我无法访问 res.text
来测试 HTML 结果:
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>');
});
});
当我尝试在 VSCode 中构建我的项目时,出现 Property 'text' does not exist on type 'Response'.
错误,这与仅显示 Response
的三个属性的自动完成一致:body
、status
和 type
。
res.body
总是 {}
所以,根据下面问题的答案,应该使用 res.text
(在 JavaScript 中):
如何在 TypeScript 中访问 res.text
?
在我的例子中,我安装了 @types/chai-http
(来自旧教程),但 https://www.npmjs.com/package/@types/chai-http 说它已被弃用。我假设在 TypeScript 中它是一个非常有限的 API,因为版本是 0.0.23(或其他)。
npm uninstall @types/chai-http
打开所有属性的自动完成,包括 .text
。这解决了我的问题。