abramenal / cypress-file-upload

File upload testing made easy
https://npm.im/cypress-file-upload
MIT License
498 stars 90 forks source link

[Bug] Cannot read property stream of null #183

Closed Mitchnsun closed 4 years ago

Mitchnsun commented 4 years ago

Current behavior:

I tried to test the upload of a CSV file. Front side, I use CSV-file-validator to read and validate my file. But I have a parsing error (see below, cannot read stream of null) that I do not have outside of cypress. During cypress test, I can console.log file (see code below) and cannot see a difference between in cypress or in a real browser...

I am not sure the issue is from cypress-file-upload library, but I only have this issue during testing with cypress (I create an issue on the repo of csv-file-validator).

Desired behavior:

CSV-file-validator could parse the file.

Steps to reproduce: (app code and test code)

Cypress: cy.get('input[type=file]').attachFile('myfile.csv'); Front:

function onFileUploaded(event) {
    const file = event.target.files[0];
    CSVFileValidator(file, CSVConfig)
      .then(csvData => {
          /* Do stuff with csvData */
      })
      .catch(err => console.error(err) });
  };
}
<input type="file" accept=".csv" onChange={onFileUploaded} />

Trace error:

TypeError: Cannot read property 'stream' of null
    at Object.parse (papaparse.min.js:46)
    at csv-file-validator.js:17
    at new Promise (<anonymous>)
    at csv-file-validator.js:16
    at Cr (AccountantFileUpload.js:64)
    at Object.<anonymous> (react-dom.production.min.js:49)
    at d (react-dom.production.min.js:69)
    at react-dom.production.min.js:73
    at k (react-dom.production.min.js:140)
    at T (react-dom.production.min.js:169)

Versions

Yarn: 1.12.3 Cypress: 4.3.0 Cypress-file-upload: 4.0.4 Csv-file-validator: 1.8.0 React: 16.6.0 MacOS: 10.14.6

Mitchnsun commented 4 years ago

I may find something, in PapaParse they have this condition: if ((global.File && _input instanceof File) || _input instanceof Object) (l.246) _input is my file in this case.

But in cypress my file is not an instance of File nor Object, that's why it cannot parse it. It seems that cypress-file-upload generate a file with a type object not Object or File It is weird, this is what I got:

Object.create(file) instanceof Object -> false
Object.create({}) instanceof Object -> true
Mitchnsun commented 4 years ago

I create a repo to reproduce easily the issue: https://github.com/Mitchnsun/bug-cypress-csv-upload

I am not sure if the issue comes from cypress-file-upload or from Cypress and how they create a Blob/File.

abramenal commented 4 years ago

Hi, thanks for submitting the issue!

Right now I am not able to take a look into the issue properly. In the meantime, I can suggest to check if the issue exist in v3 (previous major version): https://github.com/abramenal/cypress-file-upload/tree/v3.5.3 Sorry about that. Not sure I can provide ETA on solving this issue in v4 since I am the only one contributor and maintainer. If you have ideas on fixing that, please feel free to open Pull Request and we can review it together.

Thanks for understanding.

jdcl32 commented 4 years ago

this is the same issue as https://github.com/abramenal/cypress-file-upload/issues/174, that can be fixed by: https://github.com/abramenal/cypress-file-upload/pull/184

Mitchnsun commented 4 years ago

Thanks @abramenal for your answer, I tried with the v3.5.2 and the issue doesn't exist on this version. Thanks @jdcl32 for the PR

I will work with the 3.5.2 until the @jdcl32 's PR will be merged

abramenal commented 4 years ago

Hi @Mitchnsun can you please check that again in v4.0.5?

Mitchnsun commented 4 years ago

It works fine now with the v4.0.5 ! Thank you

scorp-dmitriy commented 4 years ago

For everyone who came here looking for solution, here's a workaround from https://github.com/cypress-io/cypress/issues/933#issuecomment-345554793

// nope
// this is using the global window in the test spec
new File() 

cy.window().then((win) => {
  // yup
  // this is using the File constructor from the application window
  new win.File()
})
tnrich commented 3 years ago

I'm seeing this issue in Cypress v7.1.0

Still not sure how to fix it.. I can't change the internals of how papaparse work. Has anyone got this working with papaparse specifically?

Thanks!