This is my test snippet. I need to pass additional data with nested json along with image.
`
const email = '[email protected]';
const contact = {
firstName: 'John',
lastName: 'Doe',
phone: '9876543212'
};
const response = await chai
.request(server)
.post('/profile')
.set('Content-Type', 'multipart/form-data')
.attach('logo', 'test/test-images/logo.png')
.field({
email,
name: 'All Tech Solutions',
phone: '9812345678',
contact: contact
});
expect(response.body.status).to.equal('created');
expect(response.body.profile.email).to.equal(email);
expect(response.body.profile.logo).to.exist;
});
`
It works with parameters without nested json but doesnot work with nested json. How can I pass nested json with logo?
Take a look at the
.field()method signature:And
MultipartValuetype:The value of the field should be
Blob | Buffer | fs.ReadStream | string | boolean | number;. It doesn't accept objects.So you need to use
JSON.stringify(contact).E.g.
app.js:app.test.js:test result: