Test Node lambda with Jest+aws-sdk-mock

mmontoya
1 min readMar 15, 2019

My handler function, callled save_transfer, takes a Json from the web, makes a new hashmap and save it in a SQS service, to test it:

import { save_transfer } from “../handler”;
import AWS from ‘aws-sdk-mock’;
import * as fs from “fs”;
import * as path from ‘path’;
describe(“Save Json in SQS”, () => {
it(“should save JSON in SQS”, async () => {
AWS.mock(‘SQS’, ‘sendMessage’, true);
let foopromise: any = new Promise((resolve) => {
resolve(true);
});
AWS.mock(‘SQS’, ‘promise’, foopromise);
AWS.restore(‘SQS’, ‘sendMessage’);
const jsonDummy: any = fs.readFileSync(path.join(__dirname, ‘./fixtures/webhook_dummy.json’));
const result: any = save_transfer({ body: jsonDummy.toString() });
const response: any = await result.then(function(value: any) {
return value;
});
const body: any = JSON.parse(response.body);
expect(body.message).toEqual(“Open pay Successfully processed.”);
});
});

--

--