shystruk / csv-file-validator

🔧🔦 Validation of CSV file against user defined schema (returns back object with data and invalid messages)
https://www.npmjs.com/package/csv-file-validator
MIT License
93 stars 42 forks source link

[Bug] Cannot read property stream of null #36

Closed Mitchnsun closed 4 years ago

Mitchnsun commented 4 years ago

Current behavior:

I tried to test my code with cypress and cypress-upload-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 csv-file-validator library, but I only have this issue during testing with cypress (I create an issue on the repo of cypress-file-upload). Maybe the library waiting for something that the test do not provide.

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

shystruk commented 4 years ago

Hi @Mitchnsun Can you please check if you have a File in onFileUploaded method before call CSVFileValidator

shystruk commented 4 years ago

@Mitchnsun papaparse has been upgraded. You can get the latest version 1.8.0

Mitchnsun commented 4 years ago

Hi @shystruk, thanks for your answer. I upgraded to 1.8.0 but unfortunately still have the issue. Here is what I have before calling CSVFileValidator method :

Capture d’écran 2020-04-07 à 10 02 22

Pretty much the same in browser when it works smoothly.

shystruk commented 4 years ago

hm, the validator should work in this case. Waite for the cypress-file-upload response. Can you please create an example repo or send a demo code.

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 instanceof File, that's why it cannot parse it. I transfer this comment to cypress-file-upload.

shystruk commented 4 years ago

Good point. This may be the issue. Check on cypress-file-upload what instance of File they return and probably you can make it the File instance before you pass it to the validator.

Mitchnsun commented 4 years ago

They return an object not an Object. You can reproduce the bug with this repo: 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.

I'll keep you updated

shystruk commented 4 years ago

As an idea try to get the file content from the object which cypress returns and create a File instance and pass it to the validator.

shystruk commented 4 years ago

closed the bug as it is not related to the validator

Mitchnsun commented 4 years ago

Thanks for your help, the issue was fixed by v4.0.5 of cypress-upload-file