Snapshot tests Pro / Enterprise
Snapshot tests are the easiest and most convenient way to ensure your APIs behave consistently over time. Inspired by Jest snapshot tests, Kreya's snapshot tests let you automatically capture and compare API responses, no coding required. Simply enable snapshot testing in the Kreya settings, and responses will be automatically snapshotted and compared on each run. This makes it simple to detect unexpected changes and review them before accepting.
If you need more control or want to snapshot custom values, you can use the advanced kreya.snapshot API in your scripts:
kreya.grpc.onResponse(async msg => await kreya.snapshot.verifyObjectAsJson('response', { length: msg.value.length }));
On the first run, this stores a snapshot named "response". On later runs, Kreya compares the current value to the stored snapshot and fails the test if they differ.
For most users, enabling snapshot tests in the settings is all you need. For more details on snapshot scripting, see the Kreya snapshot documentation.
Enable snapshot testing
To enable snapshot testing, go to the settings tab of an operation and enable the assertions in the Snapshot testing section:
You can also enable snapshot testing for an entire project or a specific directory, see default settings for more details.
Deterministic tests
For best results, your tests must be deterministic, meaning they produce the same output every time they run. Non-deterministic snapshots (e.g., those containing random or time-dependent data) do not work at all, since they change each time and will always fail. Avoid including such data in your API responses, or use scripting to normalize these values before snapshotting. Deterministic tests make it possible to spot real changes and avoid unnecessary snapshot updates.
Scrubbers
Kreya automatically scrubs certain values from your snapshots to keep them stable and secure. Common dynamic values such as timestamps, the local machine name, the current username, and UUIDs are replaced with placeholders. This helps ensure that your snapshots remain consistent across different environments and test runs.
Each built-in scrubber can be enabled or disabled in the settings (see also default / directory settings). You can also define custom regular-expression replacements to remove or normalize additional values.
Scrubbers (built-in and custom) are applied to operation-related snapshots only; they are not applied to invoker scripts.
If your invoker scripts generate dynamic values, normalize them in your script before calling kreya.snapshot.
If you need to scrub additional or custom values beyond the built-ins, configure custom regex replacements in settings, or use the script snapshot API to preprocess and normalize your data before snapshotting.
For example, you can remove or replace sensitive or variable fields in your script before calling kreya.snapshot.
Treat snapshots as code
Snapshots are an important part of your test suite. Treat them like code: review changes carefully, check them into version control, and keep them up to date as your APIs evolve. Reviewing snapshot diffs helps you catch unintended changes and maintain confidence in your API's behavior.
Disable formatters
Tools that rewrite files (for example Prettier) can modify Kreya-generated snapshots and cause false snapshot comparison failures. Ensure these tools ignore snapshot files.
If you use Prettier, add the following to '.prettierignore' to prevent formatting snapshots:
# Ignore Kreya snapshot files
*.snapshot.*