Cypress-file-upload: [Bug] attachFile doesn't work anymore with cypress 5.3

Created on 27 Oct 2020  Â·  10Comments  Â·  Source: abramenal/cypress-file-upload

Current behavior:

With cypress 5.3, an error pop with attachFile function that was working before with cypress 4.8. And there is no file really input at the end

image

`(0 , h[e.encoding])(...).then is not a function
C:/Users/abegey/workspace/agence-numerique/node_modules/cypress-file-upload/dist/bundle.js:1:4632

1 | var e,t=require("path"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,i=require("mime"),r=Object.freeze({subjectType:"input",force:!1,allowEmpty:!1}),o=Object.freeze({INPUT:"input",DRAG_N_DROP:"drag-n-drop"}),s={};s[o.INPUT]=["change"],s[o.DRAG_N_DROP]=["dragstart","drag","dragenter","drop","change","dragleave","dragend"];var a={message:'"subjectType" is not valid',tip:'Please look into docs to find supported "subjectType" values'},l={message:'"force" is not valid',tip:'Please look into docs to find supported "force" values'},f={message:'"allowEmpty" is not valid',tip:'Please look into docs to find supported "allowEmpty" values'},c={message:'"file encoding" is not valid',tip:'Please look into docs to find supported "encoding" values'},p={message:'"filePath" is not valid',tip:'Please look into docs to find supported "filePath" values'},u={message:'"mimeType" is not valid',tip:'Please look into docs to find supported "mimeType" values'},m={message:"given fixture file is empty",tip:'Please make sure you provide correct file or explicitly set "allowEmpty" to true'},d={message:'missing "filePath" or "fileName"',tip:'Please make sure you are passing either "filePath" or "fileName"'},y=function(e){function t(n){for(var i=[],r=arguments.length-1;r-- >0;)i[r]=arguments[r+1];e.apply(this,i),e.captureStackTrace&&e.captureStackTrace(this,t),this.name="[cypress-file-upload error]",this.message=n.message+".n"+n.tip}return e&&(t.__proto__=e),(t.prototype=Object.create(e&&e.prototype)).constructor=t,t}(Error),T=Object.freeze({ASCII:"ascii",BASE64:"base64",BINARY:"binary",HEX:"hex",LATIN1:"latin1",UTF8:"utf8",UTF_8:"utf-8",UCS2:"ucs2",UCS_2:"ucs-2",UTF16LE:"utf16le",UTF_16LE:"utf-16le"}),v=Object.freeze({JSON:"json",JS:"js",COFFEE:"coffee",HTML:"html",TXT:"txt",CSV:"csv",PNG:"png",JPG:"jpg",JPEG:"jpeg",GIF:"gif",TIF:"tif",TIFF:"tiff",ZIP:"zip",PDF:"pdf",VCF:"vcf",SVG:"svg",XLS:"xls",XLSX:"xlsx"}),g=Object.values(T),h={};h[T.ASCII]=function(e){return Cypress.Promise.resolve(e)},h[T.BASE64]=function(e,t){return Cypress.Blob.base64StringToBlob(e,t)},h[T.BINARY]=function(e,t){return Cypress.Blob.binaryStringToBlob(e,t)},h[T.HEX]=function(e){return Cypress.Promise.resolve(e)},h[T.LATIN1]=function(e){return Cypress.Promise.resolve(e)},h[T.UTF8]=function(e){return Cypress.Promise.resolve(e)},h[T.UTF_8]=function(e){return Cypress.Promise.resolve(e)},h[T.UCS2]=function(e){return Cypress.Promise.resolve(e)},h[T.UCS_2]=function(e){return Cypress.Promise.resolve(e)},h[T.UTF16LE]=function(e){return Cypress.Promise.resolve(e)},h[T.UTF_16LE]=function(e){return Cypress.Promise.resolve(e)};var P={};P[v.JSON]=T.UTF8,P[v.JS]=T.UTF8,P[v.COFFEE]=T.UTF8,P[v.HTML]=T.UTF8,P[v.TXT]=T.UTF8,P[v.CSV]=T.UTF8,P[v.PNG]=T.BASE64,P[v.JPG]=T.BASE64,P[v.JPEG]=T.BASE64,P[v.GIF]=T.BASE64,P[v.TIF]=T.BASE64,P[v.TIFF]=T.BASE64,P[v.ZIP]=T.BASE64,P[v.PDF]=T.UTF8,P[v.VCF]=T.UTF8,P[v.SVG]=T.UTF8,P[v.XLS]=T.BINARY,P[v.XLSX]=T.BINARY;var C=T.UTF8;Cypress.Commands.add("attachFile",{prevSubject:!0},function(e,T,v){var w,E,b=(void 0===(w=v)&&(w={}),void 0===(E=r)&&(E={}),Object.assign({},E,w)),F=b.subjectType,S=b.force,N=b.allowEmpty;!function(e){var t=e.subjectType,n=e.force,i=e.allowEmpty;if(-1===Object.values(o).indexOf(t))throw new y(a);if("boolean"!=typeof n)throw new y(l);if("boolean"!=typeof i)throw new y(f)}({subjectType:F,force:S,allowEmpty:N});var U,j="string"==typeof(U=T)?{filePath:U,encoding:"",mimeType:"",fileName:n.basename(U)}:{filePath:U.filePath,encoding:U.encoding||"",mimeType:U.mimeType||"",fileName:U.fileName||n.basename(U.filePath),fileContent:U.fileContent};!function(e){var t=j.filePath,n=j.fileName,i=j.encoding,r=j.mimeType,o=j.fileContent;if(i&&!g.includes(i))throw new y(c);if("string"!=typeof t&&!o)throw new y(p);if("string"!=typeof r)throw new y(u);if(!t&&!n)throw new y(d)}();var I=j.filePath,A=j.encoding,B=j.fileName,O=j.mimeType||function(e){var n=t.extname(e).slice(1);return i.getType(n)}(I),_=A||function(e){var n=t.extname(e).slice(1);return P[n]||C}(I),L=S||function(e){return!("chrome"===(t=Cypress.browser).name&&t.majorVersion<73&&function(e){if(!e)throw new Error("Element cannot be empty");return Cypress.dom.isAttached(e)&&Cypress.dom.isVisible(e)}(e))||function(e){if(!e)throw new Error("Element cannot be empty");return Cypress.dom.isDetached(e)}(e);var t}(e);return Cypress.cy.window({log:!1}).then(function(t){return function(e){var t=e.filePath,n=e.fileContent,i=e.fileEncoding;return n?Cypress.Promise.resolve(n):Cypress.cy.fixture(t,i)}({filePath:I,fileContent:j.fileContent,fileEncoding:_}).then(function(n){return function(e){var t=e.fileName,n=e.mimeType,i=e.window;return(0,h[e.encoding])(e.fileContent,n).then(function(e){return new i.File([e],t,{type:n})})}({fileContent:n,fileName:B,mimeType:O,encoding:_,window:t}).then(function(n){(function(e,t){if(!t&&0===e.size)throw new y(m)})(n,N),function(e,t){var n=t.file,i=t.subjectType,r=t.force,a=new t.window.DataTransfer;if(a.items.add(n),i===o.INPUT){var l=e[0];l.files&&l.files.length>0&&Array.prototype.forEach.call(l.files,function(e){return a.items.add(e)}),l.files=a.files}if(r){var f={bubbles:!0,cancelable:!0,detail:a};s[i].forEach(function(t){var n=new CustomEvent(t,f);Object.assign(n,{dataTransfer:a}),e[0].dispatchEvent(n)})}}(e,{file:n,subjectType:F,force:L,window:t}),Cypress.log({name:"attachFile",displayName:"FILE",message:n.name})})})}),Cypress.cy.wrap(e,{log:!1})});
| ^
2 | //# sourceMappingURL=bundle.js.map
3 | `

Desired behavior:

No error and the filed inputed correctly

Steps to reproduce: (app code and test code)

A file input present on the page and a file image.jpeg in the fixtures

cy.get("input[type=file]").attachFile("image.jpeg");

Versions

Cypress 5.3, cypress-file-upload: 4.1.1

bug

Most helpful comment

I'm getting same error/problem with cypress 6.0

All 10 comments

I tried npm i and it looks again after version update ;)

I'm getting same error/problem with cypress 6.0

Getting the similar error after updating from cypress 6.2.0 to 6.2.1; Seems like something is up with this library and how it handles updates to Cypress. I went so far as to delete my 'node_modules' folder and do an npm install, but the issue persists.

I've tried to reproduce it on CI, but no luck – all works just fine.
Can someone set up some reproducible example so I can work on the fix?

I can work on an example this weekend as I am still getting this issue with pdf files on cypress 6.2.0 +. Additionally, it appears that the file size is different from a manual upload for .pdf files. was an encoding issue.

The same bug on cypress 6.3.0

This plugin is running fine on Cypress 6.2.0, but failing on pdf uploads in Cypress 6.2.0+.
Cypress related issues: Cypress#14275 & Cypress#15038

Hey peeps, can anyone check if this issue is reproducible in this package's version?
In your package.json:

"cypress-file-upload": "github:abramenal/cypress-file-upload#feature/fix-bundle",

It tooks the code from this PR's branch: https://github.com/abramenal/cypress-file-upload/pull/285

upgrade from Cypress 6.2 --> 6.8 and changed cypress-file-upload from 5.0.2 --> "github:abramenal/cypress-file-upload#feature/fix-bundle"

this resulted in
image

Should be resolved in v5.0.4, but feel free to reopen if you still observe this.

Was this page helpful?
0 / 5 - 0 ratings