I have an express POST route set-up in index.js
as following
import * as Busboy from 'busboy';
public publish = async (req: Request, res: Response) => {
const busboy = new Busboy({ headers: req.headers });
const pl = { title: '' };
busboy.on('field', (fieldname, val) => {
switch (fieldname) {
case 'title':
pl.title = val;
break;
}
});
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
// Process files
});
busboy.on('finish', async () => {
// Process request
res.send({payload: pl});
});
}
In test index.test.js
using jest how do i mock this module in such a way that i can validate the response containing the form field title
sent in request?
Currently i use jest.mock('busboy');
but nothing gets invoked due to this.
jest.mock('busboy');
let service: ServiceController;
describe('Mosaic Research Capture Service', () => {
it('should publish', async () => {
service = new ServiceController();
const req = {
headers: {
'Content-Type': 'multipart/form-data'
},
body: {}
};
const res = {
send: jest.fn()
};
await service.publish(req, res);
});
});
The React client invokes this request as follows
const formData = new FormData();
formData.append('title', 'SomeTitle');
const header = {
credentials: 'include',
'Content-Type': 'multipart/form-data',
};
const response = await axios.post('/publish, formData, header);