readAsDataURL doesn't work in Jest - hogehoge666/syno_moments_slider GitHub Wiki

FileReader#readAsDataURL is used to convert images into Base64 format.

Even though the code works fine on the browser, testing the code with Jest generates the following error.

error TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

The Blob object generated by node-fetch by "request.blob()" is not compatible with FileReader#readAsDataURL. readAsDataURL thinks it's not a blob and throws a type error. I can't figure out if it's a node-fetch or FileReader API on Node.js.

A workaround is to generate a new blob if the code is running on Node.js.

            if (typeof process === 'undefined') {
                // For browser
                reader.readAsDataURL(blob);
            } else {
                // For passing Jest
                const blobNode = new Blob(blob.buffer, { type: blob.type });
                reader.readAsDataURL(blobNode);
            }