isBlob does not work with Jest and Jsdom
When writing test, we also using the services generated by the codegen. Works great so far! 👍🏼
At some point we realized that some tests regarding file upload failed but the code worked in the browser.
We identified that the issue seems to be how a Blob/File is detected in isBlob.
While this code works in the browser, it does not in our tests.
const isBlob = (value: any): value is Blob => {
return (
typeof value === 'object' &&
typeof value.type === 'string' &&
typeof value.stream === 'function' &&
typeof value.arrayBuffer === 'function' &&
typeof value.constructor === 'function' &&
typeof value.constructor.name === 'string' &&
/^(Blob|File)$/.test(value.constructor.name) &&
/^(Blob|File)$/.test(value[Symbol.toStringTag])
);
};
The problem seems to be at
typeof value.stream === 'function' &&
typeof value.arrayBuffer === 'function' &&
Both returns false for new File in Jest with Jsdom.
Right now we copied the generated request.ts file, remove those lines and added the --request option to the codegen.
Of course it would be nicer if we don't have to do this workaround. Maybe the isBlob method has to be re-considered?
I wonder if isBlob just needs to be a bit looser, so it doesn't stop new environments from working. I have just discovered it doesn't play well with the Cloudflare workers environment.
The last condition is false when run on Cloudflare: /^(Blob|File)$/.test(value[Symbol.toStringTag]), all other are true.
Thanks for the tip about "forking" the request file, I'll have to do that now.