reactjs - 如何在玩笑中使用 File.text()?
问题描述
我有一些问题要通过一些测试。经过几个小时试图弄清楚发生了什么,我意识到 jest 有一些问题Files
我写了一个简单的测试,期望超过一个文件内容:
describe(".file test", () => {
test("jest can work whit files", async () => {
const aContent = "a_content";
const file = new File([aContent], "aFile.txt");
const fileContent = await file.text()
expect(fileContent).toBe(aContent)
})
)
那个简单的代码,在浏览器中的真实应用程序中执行时工作。但是,当我启动测试套件时,我得到一个错误:
类型错误:file.text 不是函数
30 | const aContent = "a_content";
31 | const file = new File([aContent], "aFile.txt");
> 32 | const fileContent = await file.text()
| ^
33 | expect(fileContent).toBe(aContent)
那么,我需要开玩笑的东西可以与文件一起使用吗?
解决方案
最后,感谢@EstusFlask 的评论,我找到了一种解决方法。
我嘲笑File.prototype.text
返回文件对象本身。然后我可以用FileReader
.
推荐阅读
- r - 如何转换地图窗口和数据点的 CRS 以匹配 SF 对象?
- sql - 如何在 SQL 查询中为方程定义变量
- tensorflow - 如何临时堆叠 Tensorflow 数据集的图像,然后对它们进行批处理
- jquery - 如果按钮数据属性等于 jQuery 隐藏此
- entity-framework - EFcore3.1 get values from sys.master_files and SERVERPROPERTY
- regex - 在标记之间更改子组中的分隔符并在perl 5中拆分
- karate - 在空手道中跨多个场景和场景大纲文件使用的 Oauth 令牌
- python - 在python中获取日志级别
- powerbi - 在 Power BI 中计算比率时加 1 时筛选器不起作用
- .net-core-3.0 - ListView clear() 防止显示新内容