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.”);
});
});

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

mmontoya
mmontoya

Written by mmontoya

Remote RoR/Clojure Developer at Mexico City

No responses yet

Write a response