DEV Community

Cover image for AWS Amplify: Let's add some unit tests into our pipeline
Giorgio Boa for This is Learning

Posted on

AWS Amplify: Let's add some unit tests into our pipeline

In the previous article, we saw how to set up a pipeline with AWS Amplify, but as long as things are simple everything works.
Let's complicate the pipeline, we will see how it is possible to customise the build phase by adding tests.


ℹ️ The goal is to run unit tests with Jest in the AWS Amplify pipeline.


AWS Documentation

You can edit an app's build settings in the Amplify console by choosing App settings, Build settings. The build settings are applied to all the branches in your app, except for the branches that have an amplify.yml file saved in the repository.

Reading the AWS documentation, we can find how to add the test steps in the deployment process and it seems easy.

In the amplify.yml file is possible to add the tests section πŸŽ‰

e.g amplify.yml with test phases

version: 1
frontend:
  phases:
    [...]
test:
  phases:
    preTest:
      commands:
        - *enter command*
    test:
      commands:
        - *enter command*
    postTest:
      commands:
        - *enter command*
  artifacts:
    files:
        - location
        - location
    configFilePath: *location*
    baseDirectory: *location*
Enter fullscreen mode Exit fullscreen mode

...but if we read in the detail, however, there is a small annotation πŸ₯²

You can run end-to-end (E2E) tests in the test phase of your Amplify app to catch regressions before pushing code to production. The test phase can be configured in the build specification YML.
πŸ‘‰ Currently, you can run only the Cypress testing framework during a build. πŸ‘ˆ

AWS docs

But the goal was to run unit tests and so my reaction was…

meme

Workaround

I searched a lot for a possible solution and through various tests, I found a way to get to my goal.
Here is the solution: before running the build let's run the test script, this is a simple and effective way. πŸš€

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run test && npm run build
  artifacts:
    [...]
  cache:
    paths:
      - node_modules/**/*
Enter fullscreen mode Exit fullscreen mode

Final result

I simulated both the situation with passing tests and failing tests.

Test failed

fail1

fail2

Test passed

ok1

Ok2

Final thought

πŸŽ‰ I'd say the build configuration is pretty straightforward, good job AWS Amplify team πŸ˜„

You canΒ follow me on Twitter, where I'm posting or retweeting interesting things.

I hope you enjoyed this article, don't forget to give ❀️.
Bye πŸ‘‹

Top comments (0)