Iris Classon
Iris Classon - In Love with Code

Jest Snapshot Tests With Custom Serializer for Cloudformation Template Tests

Jest Snapshot Tests With Custom Serializer for Cloudformation Template Tests

Jest snapshot tests with custom serializer for cloudformation template tests

Introduction As promised in my previous post AWS CDK Cloudformation Integration Tests Using Jest Snapshot Tests , here is a post describing how you can use a custom serializer for the snapshots to prettify the snapshots and remove or modify parts of the template that you want to omit from the comparison. When you define an asset in a construct the CDK will define some parameters containing an MD5 hash for the bucket and for the key. These are, if I understand correctly, used as a placeholder hash that will be replaced with the hash for the deployed assets once you deploy. CDK is very fond of creating a bunch of these parameters, an issue that is currently being worked on. This means, if a change modifies the hash, and the asset is references several places in your stack as is the case for our nested stack that I mentioned in the previous post, then you will get a very cluttered result when you run the snapshot tests if the hash is changed. In Jest you can skip fields in the snapshot, but not if it is a nested object. AWS has a node module for defining resources in the test that you can use to compare against a snapshot, but that requires a more setup for the tests. I decided to go with a custom serializer instead after asking our wonderful UI team for advice.

Creating a Jest custom snapshot serializer

Creating a custom serializer is easy, figuring out what to and how to remove/set defaults is a bigger challenge. Use the addSnapshotSerializer() function or define one in the config file. Read more in the documentation. The serializer object should have two methods, test and print. Print is where you want to do the cleaning, and I did it with some horrible Regex. My Regex skills could absolutely be improved! But hey, it seems to work.

Serializer example that replaces asset parameter hashes

This example replaces asset parameter hashes with [HASH REMOVED]:

expect.addSnapshotSerializer({
  test: val => typeof val === 'string',
  print: val => {
    const newVal = val.replace(/AssetParameters([A-Fa-f0-9]{64})(\w+)/, '[HASH REMOVED]');
	const newVal2 = newVal.replace(/(\w+) (\w+) for asset\s?(version)?\s?"([A-Fa-f0-9]{64})"/, '[HASH REMOVED]');
    return `"${newVal2}"`;
  },
});

For comparison, this is the diff before and after adding the custom serializer:

        1,
        Object {
        "Fn::Split": Array [
            "||",
            Object {
-               "Ref": "AssetParameterse951c62d3eda10e9a5aab2473108127ec0d75e9ff73683a344461b7b5b982ec6S3VersionKey82384293",
+               "Ref": "[HASH REMOVED]",
            },
        ],
        },
    ],
    },

You can find some examples in my GitHub repository here

Comments

Leave a comment below, or by email.

Last modified on 2020-08-17

comments powered by Disqus