Michael Coxon
Michael Coxon

Reputation: 3535

Howto pass correct JSON Event in Jest test of AWS Lambda local invocation

I have a lambda (in node.js), which works in production:

'use strict';

const AWS = require('aws-sdk');
const Politician = require('../model/politician.js');

const dynamoDb = new AWS.DynamoDB.DocumentClient();

module.exports.put = async (event, context) => {

    const requestBody = new Politician(JSON.parse(event.body));

    return await submit(requestBody)
        .then(res => {
            return {
                statusCode: 200,
                body: JSON.stringify({
                    message: `Successfully submitted politician with name ${requestBody.name}`,
                    politicianId: res.id
                })
            }

        })
        .catch(err => {
            return {
                statusCode: 500,
                body: JSON.stringify({
                    message: `Error while submitting politician with name ${requestBody.name}`,
                })
            }
        });

};

const submit = politician => {

    return new Promise((resolve, reject) => {
        if (politician) {
            resolve(politician);
        } else {
            reject(new Error('it all went bad!'));
        }
    });
};

The problem arises when trying to set up a local test of the Lambda (I am using the Serverless framework). The problem is that I can't seem to feed any format of the event that doesn't either produce:

SyntaxError: Unexpected token u in JSON at position 0

      12 |     console.log(typeof event.body);
      13 | 
    > 14 |     const requestBody = new Politician(JSON.parse(event.body));
         |                                             ^
      15 | 
      16 | 
      17 |     return await submit(requestBody)

          at JSON.parse (<anonymous>)
      at Object.parse [as put] (functions/coxon-put-politician.js:14:45)
      at Object.put (functions-test/coxon-put-politician.test.js:37:37)

so its undefined, when I do this:

'use strict';
const AWS = require('aws-sdk');
const options = {
    region: 'localhost',
    endpoint: 'http://localhost:8000'
};
AWS.config.update(options);

const eventStub = require('../events/graham.richardson.json');
const lambda = require('../functions/coxon-put-politician');

describe('Service politicians: mock for successful operations', () => {

    test('Replies back with a JSON response', async () => {
        const event = '{"body":' + JSON.stringify(eventStub) + '}';
        const context = {};

        const result = await lambda.put(event, context);

        console.log(data);
        expect(result).toBeTruthy();
        expect(result.statusCode).toBe(200);
        expect(result.body).toBe(`{"result":"${result}"}`);
        expect(result.body.message.toContain('Successfully submitted politician'))

    });
});

or produces:

SyntaxError: Unexpected token o in JSON at position 1

      12 |     console.log(typeof event.body);
      13 | 
    > 14 |     const requestBody = new Politician(JSON.parse(event.body));
         |                                             ^
      15 | 
      16 | 
      17 |     return await submit(requestBody)

          at JSON.parse (<anonymous>)
      at Object.parse [as put] (functions/coxon-put-politician.js:14:45)
      at Object.put (functions-test/coxon-put-politician.test.js:38:37)

when I try this:

'use strict';
const AWS = require('aws-sdk');
const options = {
    region: 'localhost',
    endpoint: 'http://localhost:8000'
};
AWS.config.update(options);

const eventStub = require('../events/graham.richardson.json');
const lambda = require('../functions/coxon-put-politician');

describe('Service politicians: mock for successful operations', () => {

    test('Replies back with a JSON response', async () => {
        const event = { body: eventStub };
        const context = {};

        const result = await lambda.put(event, context);

        expect(result).toBeTruthy();
        expect(result.statusCode).toBe(200);
        expect(result.body).toBe(`{"result":"${result}"}`);
        expect(result.body.message.toContain('Successfully submitted politician'))

    });
});

So it seems that either way I go, I get an error. So what do I pass into the test as the event, so that JSON.parse(event) works?

Upvotes: 1

Views: 2425

Answers (1)

noetix
noetix

Reputation: 4923

TL;DR: const event = { body: JSON.stringify(eventStub) }

Your production code works because the code is written correctly for the expected payload structure, where event is an object and event.body is a JSON-parsable string.

In your first test you're passing event not as an object but a JSON-parsable string. event.body is undefined because event as a string doesn't have body as a parameter.

Your test should be const event = { body: JSON.stringify(eventStub) }, note that its an object with a body attribute that is a string.

In your second attempt you're passing in an object, and then when you try to JSON.parse() the object it's throwing an error.

Pay attention to the error:

Unexpected token o in JSON at position 1

The o is position 1 at object..., like u is position one for undefined.

Upvotes: 3

Related Questions