3

Background

I was thrown on this project to help alleviate some stress. The trouble is no one else has done this either so I'm pioneering the cause.

What I know

I can get lambda function output locally with:

serverless invoke local -f getArticlesById -p localfile.json -s dev

and it returns a JSON article as expected.

Question

I'm using Jasmine to test my javascript lambda functions. How can I unit test these serverless environment functions locally?

Current Attempt

My lambda function is in articles/articleHandler.js. I have a test/articles.js that runs jasmine tests leveraging lambda-tester functions. Whenever I run one of these tests I get the error

TypeError: Invalid hosts config. Expected a URL, an array of urls, a host config object, or an array of host config objects.
    at new Transport (/Users/Jackson/Sites/serverless-content/node_modules/elasticsearch/src/lib/transport.js:59:13)
    at new EsApiClient (/Users/Jackson/Sites/serverless-content/node_modules/elasticsearch/src/lib/client.js:57:22)
    at Function.Client (/Users/Jackson/Sites/serverless-content/node_modules/elasticsearch/src/lib/client.js:101:10)
    at Object.<anonymous> (/Users/Jackson/Sites/serverless-content-distribution-api-v2/elasticSearch.js:6:42)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)

I've found that this is caused by including the lambda function into the test. When I comment out that line I don't get the error. I'm guessing that because this is not a serverless call, Elasticsearch knows nothing of my environment.

test/article.js

console.log("testing articles")

const LambdaTester = require("lambda-tester");

const articleHandler = require("../articles/articleHandler.js");

describe("articles getID()", function() {
    it("test success", function() {
        return LambdaTester(articleHandler.getID)
            .event({pathParameters:{id:5633415102001}})
            .expectResult(result => {
                expect(result.body.data.id).to.be(5633415102001)
            });
    });
})
describe("articles getList()", function() {
    it("test success", function() {
        return LambdaTester(articleHandler.getList)
            .event()
            .expectResult(reset => {
                expect(result.body.data.length).to.be(10);
            });
    });
});

** ADDITIONAL **

It's looking like lambda-tester is supposed to alleviate the problem I'm encountering. Will find out more today.

2 Answers 2

3

Use lamba-tester, there are examples on the github page.

I wrote a simple lambda test function and then tested the output with jasmine + lambda-tester

As for my code, I'll need to refactor the handler someone else wrote before it will work. My simple test looks like:

Serverless yml

  testLambda:
    handler: test/testLambda.getValueOfA
    role: arn:aws:iam::367839381035:role/CodeStarWorker-fx-srch-api-v1-Lambda
    events:
      - http:
          path: test/testLambda/{a}
          method: get

Lambda Function

module.exports.getValueOfA = (event, context, callback) => {
    let a = 2;

    if(event 
        && event.pathParameters 
        && !isNaN(event.pathParameters.a)
    ) a = event.pathParameters.a;

    a = a+a;

    let ret = "the value of a is " + a;

    callback(null, ret);
}

Test

const LambdaTester = require("lambda-tester");
const TestLambda = require("./testLambda.js");

describe("testLambda()", function() {
    it("test success", function() {
        let ret;
        LambdaTester(TestLambda.getValueOfA)
            .event()
            .expectResult(result => {
                console.log(result);
                expect(result).toEqual("the value of a is 4");
            });
    });
});

I was going to set this up for parameters but didn't get there. Granted this is enough to get anyone moving forward.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried lambda-tester and it looks like it certainly does the job great. However, is it possible to debug the lambda functions (with setting the breakpoints in VSCode) while the lambda-tester tests are running? I couldn't find a way to do it so far, it would be the last piece of the puzzle from my point of view.
0

another option you have is to call the function directly from your test. In the end, it's nothing more than a function, so you can import the module and call it, passing the right parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.