From efa9358efb41bb3ccadfc0632fe396d082d388b1 Mon Sep 17 00:00:00 2001 From: Emil Ong Date: Thu, 20 Aug 2020 07:39:22 -0700 Subject: [PATCH] Remove thenability from Cypress.Blob >= 5.0.0 --- lib/file/getFileBlobAsync.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/file/getFileBlobAsync.js b/lib/file/getFileBlobAsync.js index 74f66ac..0f2ac35 100644 --- a/lib/file/getFileBlobAsync.js +++ b/lib/file/getFileBlobAsync.js @@ -2,12 +2,24 @@ import { extname } from 'path'; import { ENCODING, FILE_EXTENSION } from './constants'; +let wrapBlob = blob => { + // Cypress version 5 assigns a function with a compatibility warning + // to blob.then, but that makes the Blob actually thenable. We have + // to remove that to Promise.resolve not treat it as thenable. + // + // eslint-disable-next-line no-param-reassign + delete blob.then; + return Cypress.Promise.resolve(blob); +}; + +if (Cypress.version < '5.0.0') { + wrapBlob = blob => blob; +} + const ENCODING_TO_BLOB_GETTER = { [ENCODING.ASCII]: fileContent => Cypress.Promise.resolve(fileContent), - [ENCODING.BASE64]: (fileContent, mimeType) => - Cypress.Promise.try(() => Cypress.Blob.base64StringToBlob(fileContent, mimeType)), - [ENCODING.BINARY]: (fileContent, mimeType) => - Cypress.Promise.try(() => Cypress.Blob.binaryStringToBlob(fileContent, mimeType)), + [ENCODING.BASE64]: (fileContent, mimeType) => wrapBlob(Cypress.Blob.base64StringToBlob(fileContent, mimeType)), + [ENCODING.BINARY]: (fileContent, mimeType) => wrapBlob(Cypress.Blob.binaryStringToBlob(fileContent, mimeType)), [ENCODING.HEX]: fileContent => Cypress.Promise.resolve(fileContent), [ENCODING.LATIN1]: fileContent => Cypress.Promise.resolve(fileContent), [ENCODING.UTF8]: fileContent => Cypress.Promise.resolve(fileContent),