azure-devopsjestjscontinuous-integrationazure-pipelinesjest-image-snapshot

How to do jest snapshots within Azure Pipelines


I am using jest snapshots, as well as jest-image-snapshots. They work locally, but when I run them in the azure pipeline I get:

    New snapshot was not written. The update flag must be explicitly passed to write a new snapshot.

    This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default.

Wouldn't updating the snapshots on every run defeat the purpose?

I've researched a bit but the best solutions I came to were updating snapshots, or removing CI=true. Removing CI=true did not work, and I don't see how updating snapshots could be a good solution.

This is happening for both unit tests, as well as image-snapshots within integration tests.

Any clarification or alternative solutions are appreciated.


Solution

  • In a CI system, such as Azure DevOps, Jests snapshots will not be written automatically unless pass --updateSnapshot explicitly to tell it to regenerate snapshots.

    To resolve this, you can run the following script:

    jest --updateSnapshot
    

    What's more, it is recommended to put snapshots in the repository rather than regenerate them in CI.

    If there are no special requirements, you can firstly run snappshots locally and put the .snap files to the repository, then run on CI.