title: Using AWS CodeBuild or Lambda to run jasmine tests before deployment
body: <p>Folks,
    Have been trying to figure out the correct way to fire of a CodeBuild project which either produces the artifact after compiling and running jasmine tests, or fails and stops the CodePipeline from proceeding with deployment.</p>
  
  <p>If my <code>buildspec.yml</code> looks like:</p>
  
  <pre><code>version: 0.1
  
  phases:
    install:
      commands:
        - echo Installing... Running npm install
        - npm install
    pre_build:
      commands:
        - echo pre_build...
    build:
      commands:
        - echo Testing... Running npm test
        - npm test
    post_build:
      commands:
        - echo Build completed on `date`
  artifacts:
    files:
      - '**/*'
  </code></pre>
  
  <p>How should I fail out of the <code>npm test</code> phase?  If any of the jasmine tests fail during <code>npm test</code>, will the artifact still be produced?  </p>
  
  <p>Another thought I have is to have the following occur if any of the tests fail:</p>
  
  <pre><code>    var params = {
          jobId: jobId,
          failureDetails: {
              message: JSON.stringify(message),
              type: 'JobFailed',
              externalExecutionId: context.invokeid
          }
      };
      codepipeline.putJobFailureResult(params, function(err, data) {
          ...
      });
  </code></pre>
  
  <p>or send a stop signal to CodeBuild?</p>
  
  <pre><code>var params = {
    id: 'STRING_VALUE' /* required */
  };
  codebuild.stopBuild(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  });
  </code></pre>
  
  <p>Or how do I terminate the build to not produce the artifact?  Maybe I got this wrong, and it should be a Lambda function that kicks off the unit tests.  I am not sure if Lambda is ideal for this, as I can imagine some services take a while to finish the unit tests</p>
  
  <p>Thanks!</p>