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.