Javascript text()方法在Blob中不可用

Javascript text()方法在Blob中不可用,javascript,axios,jestjs,blob,nock,Javascript,Axios,Jestjs,Blob,Nock,我正在使用&to测试链接进行一些集成测试。 在其中一个示例中,我希望收到一个Blob实例 根据,我应该能够使用它的text()方法。 我的问题是:为什么这个方法没有定义?(在我的测试中) 以下是(简化)代码: //interceptors.js const useBlobTextProperty=async(响应)=>{ const{data:myBlob}=response console.log(myBlob.toString())//打印“[object Blob]” response.r

我正在使用&to测试链接进行一些集成测试。
在其中一个示例中,我希望收到一个
Blob
实例
根据,我应该能够使用它的
text()
方法。
我的问题是:为什么这个方法没有定义?(在我的测试中)

以下是(简化)代码:

//interceptors.js
const useBlobTextProperty=async(响应)=>{
const{data:myBlob}=response
console.log(myBlob.toString())//打印“[object Blob]”
response.rawText=wait myBlob.text()//类型错误:myBlob.text不是函数
返回响应
}
//foo.test.js
从“axios”导入axios
从“axios/lib/adapters/http”导入httpAdapter
从“nock”导入nock
const testAxios=axios.create({
响应编码:“utf8”,
responseType:'blob',
})
testAxios.interceptors.response.use(useBlobTextProperty,null)
常数主机http://localhost'
axios.defaults.host=主机
axios.defaults.adapter=httpAdapter
描述('SO测试',()=>{
在每个之前(()=>{
nock(主机)
.defaultReplyHeaders({'Content-Type':'application/json'})
.persist()
.get(“/”)
.答复(200,{foo:17})
})
之后(()=>{
nock.cleanAll()
})
它('should get raw text',async()=>{
const returnValue=await testAxios.get('/')//FAIL:TypeError
expect(returnValue.rawText).toEqual(“{”foo:17}”)
}, 1000)
})
仅供参考,为了解决此问题,我正在使用另一个拦截器:

//interceptors.js
const addMissingTextPropertyToBlob=(响应)=>{
const{data}=响应
if(data.toString()=='[object Blob]'&&&!data.text){
data.text=()=>
新承诺((解决、拒绝)=>{
const reader=new FileReader()
reader.onload=()=>{resolve(reader.result)}
reader.onerror=(错误)=>{拒绝(错误)}
reader.readAsText(数据)
})
}
返回响应
}
//foo.test.js
//响应拦截器从索引0到N-1执行
testAxios.interceptors.response.use(addMissingTextPropertyToBlob,null)
testAxios.interceptors.response.use(useBlobTextProperty,null)
//然后,它工作了!

但是我想理解为什么我不能在测试中依赖于
Blob.text()

您在哪里运行此代码?不是普遍支持。我正在本地计算机上通过VS代码运行此代码。当我在real中运行我的代码时,它在Chrome、Edge和Firefox上都运行良好(即:没有
addMissingTextPropertyToBlob
)。VS代码在哪里运行代码?如果您使用的是Nock,我假设您在节点中运行此代码。请记住,节点不具有对Blob的本机支持。因此,Axios在提供
responseType:'Blob'
时,正在做一些奇怪的事情来模拟Blob……或者您只需使用该nock库打开一个功能请求来实现完整的API。正如您所演示的,这并不难。您在哪里运行此代码?不是普遍支持。我正在本地计算机上通过VS代码运行此代码。当我在real中运行我的代码时,它在Chrome、Edge和Firefox上都运行良好(即:没有
addMissingTextPropertyToBlob
)。VS代码在哪里运行代码?如果您使用的是Nock,我假设您在节点中运行此代码。请记住,节点不具有对Blob的本机支持。因此,Axios在提供
responseType:'Blob'
时,正在做一些奇怪的事情来模拟Blob……或者您只需使用该nock库打开一个功能请求来实现完整的API。正如你所展示的,这并不难。