I am doing some Integration tests with jest
& nock
to test Axios interceptors chaining.
In one of them, I am expecting to receive a Blob
instance
and according to this link, I should be able to use its text()
method.
My question is: Why is this method not defined? (in my tests)
Here is the (simplified) code:
// interceptors.js
const useBlobTextProperty = async (response) => {
const { data: myBlob } = response
console.log(myBlob.toString()) // Prints "[object Blob]"
response.rawText = await myBlob.text() // TypeError: myBlob.text is not a function
return response
}
// foo.test.js
import axios from 'axios'
import httpAdapter from 'axios/lib/adapters/http'
import nock from 'nock'
const testAxios = axios.create({
responseEncoding: 'utf8',
responseType: 'blob',
})
testAxios.interceptors.response.use(useBlobTextProperty, null)
const host = 'http://localhost'
axios.defaults.host = host
axios.defaults.adapter = httpAdapter
describe('SO test', () => {
beforeEach(() => {
nock(host)
.defaultReplyHeaders({ 'Content-Type': 'application/json' })
.persist()
.get('/')
.reply(200, { foo: 17 })
})
afterEach(() => {
nock.cleanAll()
})
it('should get raw text', async () => {
const returnValue = await testAxios.get('/') // FAIL: TypeError
expect(returnValue.rawText).toEqual('{"foo":17}')
}, 1000)
})
FYI, to workaround this issue, I am using another interceptor:
// interceptors.js
const addMissingTextPropertyToBlob = (response) => {
const { data } = response
if (data.toString() === '[object Blob]' && !data.text) {
data.text = () =>
new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => { resolve(reader.result) }
reader.onerror = (error) => { reject(error) }
reader.readAsText(data)
})
}
return response
}
// foo.test.js
// Response interceptors are executed from index 0 to N-1
testAxios.interceptors.response.use(addMissingTextPropertyToBlob, null)
testAxios.interceptors.response.use(useBlobTextProperty, null)
// Then, it works!
But I'd like to understand why I cannot rely on Blob.text()
in my tests.
text
method ofBlob
is not universally supported. – ProbityaddMissingTextPropertyToBlob
) on both Chrome, Edge & Firefox. – Tetrasyllableyarn test
(i.e.:npx jest
) in the VS Code's terminal of my machine (Windows 10 Pro) – Tetrasyllabletest
script in your package.json do? – Probity"test": "jest"
– Tetrasyllablefoo.test.js
in a node.js environment, not in a browser?! – ProbityresponseType: 'blob'
is provided. – BenemptaddMissingTextPropertyToBlob
is the right way to correctly set up my tests. Thanks. I let you write the proper answer, otherwise I can/will do it myself! – Tetrasyllable