如何在玩笑中使用 File.text() ?
How to use File.text() in jest?
我有一些问题要通过一些测试。花了几个小时试图弄清楚发生了什么之后,我意识到 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)
})
)
这么简单的代码,当在浏览器中的实际应用程序中执行时就可以工作了。但是,当我启动测试套件时,我得到一个错误:
TypeError: file.text is not a function
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
模拟到 return 文件对象本身。然后我可以使用 FileReader
.
阅读其测试内容
我有一些问题要通过一些测试。花了几个小时试图弄清楚发生了什么之后,我意识到 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)
})
)
这么简单的代码,当在浏览器中的实际应用程序中执行时就可以工作了。但是,当我启动测试套件时,我得到一个错误:
TypeError: file.text is not a function
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
模拟到 return 文件对象本身。然后我可以使用 FileReader
.