简体   繁体   English

使用Jest和Lambda-Tester对AWS Lambda进行单元测试

[英]Unit test AWS Lambda with jest and lambda-tester

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. 在线上有很多关于如何测试AWS Lambda以及如何模拟某些依赖项的帖子。也许我已经简化了,但是我不需要任何简化。 For a while, I have been using mocha/chai and lambda-tester . 一段时间以来,我一直在使用mocha / chai和lambda-tester This worked just fine for running tests with simple npm test . 这对于使用简单的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). 我现在的问题是,由于我将开始使用Jest而不是mocha / chai,因此我更新了所有测试以匹配Jest语法(与大多数语法非常相似)。 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. 为简单起见,我的Lambda之一只是返回了图片网址。 My test should verify that has a statusCode:200 and that headers has a content-type property. 我的测试应验证是否具有statusCode:200以及标头具有content-type属性。

Here is my test as it was in mocha/chai format, when it consistently passed (verified not false positives): 这是我通过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' );
      });
  });
})

Fairly straight forward. 非常坦率的。 Now, here is my updated test in Jest, which is inconsistent: 现在,这是我在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' );
    });
});

It is possible that I have missed something in my conversion to Jest, but I cannot see what. 转换为Jest的过程中可能遗漏了一些东西,但看不到。 Hopefully someone can spot my mistake and help me move forward. 希望有人能发现我的错误并帮助我前进。

There seems to be nothing wrong with your code. 您的代码似乎没有错。

One of the biggest difference between mocha and jest is that jest runs tests concurrently and therefore it can detect issues like race conditions and mutations outside your handler on your Lambda that mocha may miss. mochajest之间的最大区别之一是, jest同时运行测试,因此它可以检测出竞争状况以及Lambda上的处理程序外部的突变等可能导致mocha丢失的问题。

Here's I would troubleshoot it: 这是我要解决的问题:

  • Try to run that single test multiple times (that single file and that single test using test.only ). 尝试多次运行该单个测试(该单个文件和该单个测试使用test.only )。
  • Try running the entire suite serially using jest --runInBand . 尝试使用jest --runInBand串行运行整个套件。

If we get consistent failures with those, then you probably have race condition issues and/or maybe a bug in your code where you store state outside of your handler and it is being shared by different invocations. 如果我们遇到一致的失败,那么您可能会遇到竞态条件问题和/或代码中的错误,其中您将状态存储在处理程序之外,并且状态由不同的调用共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM