I have this lambda function in which I am creating a new connection,
export const create = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
const validation = validate(data);
if (!validation.ok) {
console.error('Validation Failed');
callback(null, {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: "Couldn't create the connection. \n - " + validation.message,
});
return;
}
const params = {
TableName: process.env.CONNECTIONS_TABLE ?? '',
Item: {
id: uuidv4(),
providerName: data.providerName,
apiKey: data.apiKey,
apiSecret: data.apiSecret,
search: data.search,
attributes: data.attributes,
createdAt: timestamp,
updatedAt: timestamp,
},
};
dynamodb.put(params, (error) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: "Couldn't create the connection.",
});
return;
}
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify(params.Item),
};
callback(null, response);
});
};
and I have wrote unit test for this as below,
describe('Connections API', () => {
it('should create item via api', async () => {
const newConnection = await new Promise((resolve, reject) => {
Create.create(createEvent, context, (err, callback) => {
if (err) {
reject(`match ${err}`);
} else {
resolve(`match ${callback}`);
}
const getCreatedAt = JSON.parse(callback.body).createdAt;
const getUpdatedAt = JSON.parse(callback.body).updatedAt;
const getId = JSON.parse(callback.body).id;
const getConnnection = JSON.stringify({
id: getId,
providerName: 'test',
apiKey: 'test',
apiSecret: 'test',
search: {
origin: {
location: { latitude: 44.599284, longitude: -115.980451 },
deadheadRadius: { value: 500, unit: 'Miles' },
},
destination: {
location: { latitude: 42.9316, longitude: -88.6921 },
deadheadRadius: { value: 500, unit: 'Miles' },
},
equipmentType: 'V',
mode: 'TL',
},
attributes: {},
createdAt: getCreatedAt,
updatedAt: getUpdatedAt,
});
expect(callback.statusCode).toEqual(200);
expect(callback.body).toEqual(getConnnection);
});
});
});
Here arguments passed to create function are are mocked in other folder but I have just created mock object for argument createEvent
But still I am not able to figure out what I am doing wrong, I know that I need to use aws-sdk-mock package for mocking a function.
Please help me understand this
Thank you in advance