Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
How to handle file upload if FormData when testing using Cypress?

I'm using VueJS and Cypress. I have a modal where I submit a FormData with different filled and a file:

var formData = new FormData();
formData.append("document_file", this.documentFile);
formData.append("comments", this.comments.value);
...

They way I upload it:

this.$http.post('http://localhost:8081/api/upload',formData,{emulateJSON: true},{
    header:{ "Content-Type":"multipart/form-data" },
    }).then(function(response) {
        // ... Code
    }).catch((err) => {
        // .. Code
    });
}

I want to use Cypress to test the params. Now, when I don't use the document file in the formData, I have the following methods to parse the multipart/form-data; boundary=---:

function parse(request) {
  console.log("request");
  console.log(request);
  const headers = request.headers;
  const body = request.body;
  const content_type = headers['content-type'];
  expect(content_type, 'boundary').to.match(/^multipart\/form-data; boundary=/);
  const boundary = content_type.split('boundary=')[1];
  const values = p(boundary, body);
  return values;
}

function p(boundary, body) {
  expect(boundary, 'boundary').to.be.a('string');
  expect(body, 'body').to.be.a('string');
  const parts = body.split(`--${boundary}`).map((s) => s.trim()).filter((s) => s.startsWith('Content-Disposition: form-data;'));
  const result = {};
  parts.forEach((part) => {
    const lines = part.split(/\r?\n/g);
    const key = lines[0].match(/name="(.+)"/)[1];
    result[key] = (lines.length >= 2) ? lines[2].trim() : "";
  });
  return result;
}

Which work great. But when I upload the file, I get a different request.body:

enter image description here

They way I'm trying to test:

cy.get('@createDocument').then((interception) => {                                                                   
  assert.isNotNull(interception);
  const values = parse(interception.request);
  assert.isTrue(values.comments === "");
});

How can I handle this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!