How to get environment variables defined in serverless.yml in tests









up vote
2
down vote

favorite
1












I am using the serverless framework for running lambda functions on AWS.



In my serverless.yml there are environment variables that are fetched from SSM.



When I write integration tests for the code, I need the code to have the environment variables and I can't find a good way to do this.



I don't want to duplicate all the variables definitions just for the tests, they are already defined in the serverless.yml. Also, some are secrets and I can't commit them to source conrol, so I would have to also repeat them in the ci environment.



Tried using the serverless-jest-plugin but it is not working and not well maintained.



Ideas I had for solutions:



  1. Make the tests exec sls invoke - this will work but would mean that the code cannot be debugged, I won't know the test coverage, and it will be slow.

  2. Parse the serverless.yml myself and export the env variables - possible but rewriting the logic of pulling the SSM variables just for tests seems wrong.

Any ideas?










share|improve this question



























    up vote
    2
    down vote

    favorite
    1












    I am using the serverless framework for running lambda functions on AWS.



    In my serverless.yml there are environment variables that are fetched from SSM.



    When I write integration tests for the code, I need the code to have the environment variables and I can't find a good way to do this.



    I don't want to duplicate all the variables definitions just for the tests, they are already defined in the serverless.yml. Also, some are secrets and I can't commit them to source conrol, so I would have to also repeat them in the ci environment.



    Tried using the serverless-jest-plugin but it is not working and not well maintained.



    Ideas I had for solutions:



    1. Make the tests exec sls invoke - this will work but would mean that the code cannot be debugged, I won't know the test coverage, and it will be slow.

    2. Parse the serverless.yml myself and export the env variables - possible but rewriting the logic of pulling the SSM variables just for tests seems wrong.

    Any ideas?










    share|improve this question

























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I am using the serverless framework for running lambda functions on AWS.



      In my serverless.yml there are environment variables that are fetched from SSM.



      When I write integration tests for the code, I need the code to have the environment variables and I can't find a good way to do this.



      I don't want to duplicate all the variables definitions just for the tests, they are already defined in the serverless.yml. Also, some are secrets and I can't commit them to source conrol, so I would have to also repeat them in the ci environment.



      Tried using the serverless-jest-plugin but it is not working and not well maintained.



      Ideas I had for solutions:



      1. Make the tests exec sls invoke - this will work but would mean that the code cannot be debugged, I won't know the test coverage, and it will be slow.

      2. Parse the serverless.yml myself and export the env variables - possible but rewriting the logic of pulling the SSM variables just for tests seems wrong.

      Any ideas?










      share|improve this question















      I am using the serverless framework for running lambda functions on AWS.



      In my serverless.yml there are environment variables that are fetched from SSM.



      When I write integration tests for the code, I need the code to have the environment variables and I can't find a good way to do this.



      I don't want to duplicate all the variables definitions just for the tests, they are already defined in the serverless.yml. Also, some are secrets and I can't commit them to source conrol, so I would have to also repeat them in the ci environment.



      Tried using the serverless-jest-plugin but it is not working and not well maintained.



      Ideas I had for solutions:



      1. Make the tests exec sls invoke - this will work but would mean that the code cannot be debugged, I won't know the test coverage, and it will be slow.

      2. Parse the serverless.yml myself and export the env variables - possible but rewriting the logic of pulling the SSM variables just for tests seems wrong.

      Any ideas?







      node.js integration-testing jestjs serverless-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked Nov 9 at 20:15









      brafdlog

      1,4741108




      1,4741108






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Are you looking to do mocked unit tests, or something more like integration tests?



          In the first case, you don't need real values for the environment variables. Mock your database, or whatever requires environment variables set. This is actually the preferable way because the tests will run super quickly with proper mocks.



          If you are actually looking to go with end-to-end/integration kind of approach, then you would do something like sls invoke, but from jest using javascript. So, like regular network calls to your deployed api.



          Also, I would recommend not to store keys in serverless.yml. Try the secret: $env:MY_SECRET syntax instead (https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables), and use environment variables instead. If you have a ci/cd build server, you can store your secrets there.






          share|improve this answer




















          • Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
            – brafdlog
            yesterday











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232729%2fhow-to-get-environment-variables-defined-in-serverless-yml-in-tests%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Are you looking to do mocked unit tests, or something more like integration tests?



          In the first case, you don't need real values for the environment variables. Mock your database, or whatever requires environment variables set. This is actually the preferable way because the tests will run super quickly with proper mocks.



          If you are actually looking to go with end-to-end/integration kind of approach, then you would do something like sls invoke, but from jest using javascript. So, like regular network calls to your deployed api.



          Also, I would recommend not to store keys in serverless.yml. Try the secret: $env:MY_SECRET syntax instead (https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables), and use environment variables instead. If you have a ci/cd build server, you can store your secrets there.






          share|improve this answer




















          • Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
            – brafdlog
            yesterday















          up vote
          0
          down vote













          Are you looking to do mocked unit tests, or something more like integration tests?



          In the first case, you don't need real values for the environment variables. Mock your database, or whatever requires environment variables set. This is actually the preferable way because the tests will run super quickly with proper mocks.



          If you are actually looking to go with end-to-end/integration kind of approach, then you would do something like sls invoke, but from jest using javascript. So, like regular network calls to your deployed api.



          Also, I would recommend not to store keys in serverless.yml. Try the secret: $env:MY_SECRET syntax instead (https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables), and use environment variables instead. If you have a ci/cd build server, you can store your secrets there.






          share|improve this answer




















          • Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
            – brafdlog
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          Are you looking to do mocked unit tests, or something more like integration tests?



          In the first case, you don't need real values for the environment variables. Mock your database, or whatever requires environment variables set. This is actually the preferable way because the tests will run super quickly with proper mocks.



          If you are actually looking to go with end-to-end/integration kind of approach, then you would do something like sls invoke, but from jest using javascript. So, like regular network calls to your deployed api.



          Also, I would recommend not to store keys in serverless.yml. Try the secret: $env:MY_SECRET syntax instead (https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables), and use environment variables instead. If you have a ci/cd build server, you can store your secrets there.






          share|improve this answer












          Are you looking to do mocked unit tests, or something more like integration tests?



          In the first case, you don't need real values for the environment variables. Mock your database, or whatever requires environment variables set. This is actually the preferable way because the tests will run super quickly with proper mocks.



          If you are actually looking to go with end-to-end/integration kind of approach, then you would do something like sls invoke, but from jest using javascript. So, like regular network calls to your deployed api.



          Also, I would recommend not to store keys in serverless.yml. Try the secret: $env:MY_SECRET syntax instead (https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables), and use environment variables instead. If you have a ci/cd build server, you can store your secrets there.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 22:12









          Herman Starikov

          727315




          727315











          • Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
            – brafdlog
            yesterday

















          • Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
            – brafdlog
            yesterday
















          Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
          – brafdlog
          yesterday





          Im am looking for a solution for integration tests. Updated the question to clarify. Thanks
          – brafdlog
          yesterday


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232729%2fhow-to-get-environment-variables-defined-in-serverless-yml-in-tests%23new-answer', 'question_page');

          );

          Post as a guest














































































          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3