Automating Linting and Testing
Enhancing code quality and functionality is made easier with automated linting and testing tools. In this example, we use automated tests and linters to ensure our code meets best practices and functions as intended. For instance, consider the following scenario. When you execute these commands:npm run test—correctly report whether the code passes or fails. After executing tests, you might see output similar to:
num1 + num2) causes the tests to pass again.
Linting, on the other hand, inspects your code for adherence to best practices. For example, the following code:
npm run lint) may reveal issues. For example, errors like an undefined variable named test or expect, and an unused variable (body), are flagged:
npm run lint confirms that the code adheres to established best practices.
To ensure every change is validated, developers should run both linting and tests. By pushing code to CodeCommit, you can configure CodeBuild to run these checks automatically, streamlining your deployment process.
Setting Up the Repository and Pushing Code
Begin by creating a repository in CodeCommit. For instance, name the repository “webapp”. Follow these steps to add files to Git and push the initial commit:Configuring CodeBuild
After setting up your repository, navigate to the CodeBuild service and create a new project (e.g., “webapp-CodeBuild”). Adjust the following settings:- Source: Select CodeCommit and choose the “webapp” repository with the main branch.
- Environment: Choose a managed image. For example, run the build on EC2 with Amazon Linux using the latest Node.js version (e.g., Node.js 20).
- Service Role: Create a new service role.
- Build Specifications: Supply build commands either directly or by using a buildspec file.
buildspec.yaml file that installs dependencies, performs linting and testing, and defines post-build actions:
npm install, executing linting with npm run lint, and running tests with npm run test. If any phase fails (for instance, due to failing tests), CodeBuild halts further execution, and the build fails.
Several images below illustrate the configuration steps:





buildspec.yaml file to your repository (using git add, git commit, and git push), CodeBuild detects the file and automatically starts a new build. For example, the logs might display:



Integrating CodeBuild with CodePipeline
To fully automate your deployment process, integrate CodeBuild with CodePipeline. With this integration, every code change pushed to CodeCommit automatically triggers a CodeBuild run that performs linting and testing. If the build passes, CodePipeline then deploys the resulting artifact to your Elastic Beanstalk environment. Follow these steps to configure the pipeline:- Create a Pipeline:
In CodePipeline, create a new pipeline (for example, “webapp-pipeline”).- Source Stage: Set to CodeCommit and select your repository.
- Build Stage: Specify AWS CodeBuild and choose the “webapp-CodeBuild” project.
- Deploy Stage: Use Elastic Beanstalk as the deploy provider and select your existing application and environment.

buildspec.yaml file to define an artifact by adding an artifacts section:





npm run test to fail—CodeBuild stops the pipeline, and the previous stable version remains deployed. The logs will indicate the failure, and no new deployment is made.
Consider this example HTML snippet from before a failed deployment:
Conclusion
This lesson demonstrated how to integrate automated linting and testing using CodeBuild and how to automate these checks using CodePipeline with CodeCommit and Elastic Beanstalk. By using abuildspec.yaml file, you ensure that your code quality is verified with every commit, effectively preventing broken code from reaching production.
Happy coding and enjoy your streamlined workflow!