There are many posts online about how to test AWS lambdas, and how to mock certain dependencies etc. Maybe I am over simplifying, but I don't need any of that. For a while, I have been using mocha/chai and lambda-tester. This worked just fine for running tests with simple npm test.
My problem now is, as I am being ushered towards using Jest instead of mocha/chai, I have since updated all of my tests to match Jest syntax(fairly similar as most are). Now however, my tests sometimes pass and sometimes fail. While this makes me think my tests are not handling async correctly, I do not see how I can make use of the Jest docs and use done in my code, as I believe lambda-tester returns the result I am expecting.
To keep things simple, one of my Lambdas simply returns an image url. My test should verify that has a statusCode:200 and that headers has a content-type property.
Here is my test as it was in mocha/chai format, when it consistently passed (verified not false positives):
describe('Lambda to return imageURl: ', () => {
it('should return a status code 200 and have the correct header', () => {
return LambdaTester( lambda.handler )
.event( testEvent )
.expectSucceed( ( result ) => {
expect( result.statusCode ).to.equal(200);
expect( result.headers ).to.have.property( 'Location' );
});
});
})
Fairly straight forward. Now, here is my updated test in Jest, which is inconsistent:
test('should return a status code 200 and have the correct header', () => {
return LambdaTester( lambda.handler )
.event( testEvent )
.expectSucceed( ( result ) => {
expect( result.statusCode ).toBe(200);
expect( result.headers ).toHaveProperty( 'Content-Type' );
});
});
It is possible that I have missed something in my conversion to Jest, but I cannot see what. Hopefully someone can spot my mistake and help me move forward.