【发布时间】:2023-04-04 01:41:01
【问题描述】:
网上有很多关于如何测试 AWS lambdas 以及如何模拟某些依赖项等的帖子。也许我过于简化了,但我不需要这些。有一段时间,我一直在使用 mocha/chai 和lambda-tester。这对于使用简单的 npm test 运行测试非常有效。
我现在的问题是,当我被引导使用 Jest 而不是 mocha/chai 时,我已经更新了我的所有测试以匹配 Jest 语法(与大多数人非常相似)。然而现在,我的测试有时会通过,有时会失败。虽然这让我觉得我的测试没有正确处理异步,但我看不到如何利用 Jest 文档并在我的代码中使用 done,因为我相信 lambda-tester 返回了我期望的结果。
为了简单起见,我的一个 Lambda 只返回一个图像 url。我的测试应该验证有一个 statusCode:200 并且 headers 有一个 content-type 属性。
这是我的测试,因为它是 mocha/chai 格式,当它始终通过时(验证不是误报):
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' );
});
});
})
相当直截了当。现在,这是我在 Jest 中更新的测试,不一致:
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' );
});
});
在转换为 Jest 的过程中,我可能遗漏了一些东西,但我看不出是什么。希望有人能发现我的错误并帮助我继续前进。
【问题讨论】:
标签: unit-testing aws-lambda jestjs