typescript - 如何在 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'.
错误,这与仅显示 a 的三个属性的自动完成一致Response
:body
、、status
和type
。
res.body
总是{}
如此,根据对以下问题的回答,应该使用(res.text
在 JavaScript 中):Chai response.body 始终为空 {}
如何在 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
. 这解决了我的问题。
推荐阅读
- asp.net - 401 错误后使用 C# 调用 Dynamics NAV Web 服务成功
- react-testing-library - RTL - 处理复杂和大型集成组件超时的“正确”方式
- python - 从 Transfermarkt 抓取数据 - 如何获取全名和姓氏
- azure - 从 Azure HDInsight 获取 Spark Yarn 日志
- ios - 为什么我的自定义 UIActivity 在 UIActivityViewController 中不再可见?
- java - 如何使用上一个会话中的 cookie 打开新的 selenium chrome 驱动程序会话以绕过网站登录凭据
- c++ - 基本数据类型究竟是如何分配给特定架构的
- windows - 批处理脚本中的@var@ 和 %var% 有什么区别?
- c - 从 Ada 中的鼠标输入读取
- reactjs - 根据数据中的类型字段将数据数组映射到特定的反应组件