Is there a way to pass files in the body of the Event Object of a lambda function manually?

Viewed 273

I am trying to test a lambda function using the jest test framework. And I want to pass files in through the Event object. I am loading the file and converting to base64 with the fs module.

test.js

describe('registerApplicant', () => {
  const pic = fs.readFileSync(path.join(process.cwd(), '__tests__', 'pic.jpg')).toString('base64');
  
  const event = {
    body: JSON.stringify({
      firstName: 'SomeName',
      picture: pic
    }),
    headers: { 'content-type': 'multipart/form-data; boundary="----54645645645664564563424768""' },
    httpMethod: 'POST',
    isBase64Encoded: true,
    path: '',
    requestContext: {},
    resource: ''
  };

  it('it should test the lambda fxn', async () => {
    let response = await wrapped.run(event);
    expect(response.statusCode).toEqual(200);
  });

}

I am using lambda-multipart-parser to process the files. When I run the test it invokes my lambda with the event.body containing the body data as is.

handler.js

const parser = require('lambda-multipart-parser');

module.exports.LambdaFxn = async (event) => {
  const result = await parser.parse(event);
  console.log(result);
}

Gives:

{ files: [] }

The file array is supposed to contain parameters of the file but it is empty. It does not recognise the file stream I added in the event body. I want the file to be accessible from within the lambda. How can I achieve this?

0 Answers
Related