Tests Pro / Enterprise
Kreya makes it easy to validate API responses and ensure your workflows behave as expected. The recommended way to test in Kreya is with snapshot tests, which require no coding, just enable them in the settings and Kreya will automatically capture and compare your API responses. For more advanced scenarios, you can also define custom tests directly in your scripts.
Snapshot tests
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, see the Kreya snapshot documentation.
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.*
Scripting tests
For more advanced or custom assertions, you can use the kreya.test
function together with the popular Chai assertion library, which is bundled with the app.
Here's an example:
import { expect } from 'chai'; // Chai is pre-installed in Kreya
kreya.test('test that will fail', () => expect(2).to.eql(3));
kreya.test('test that will succeed', () => expect(6).to.eql(6));
You can combine snapshot and scripting assertions in the same script:
import { expect } from 'chai';
kreya.snapshot.verifyObjectAsJson('response', responseData);
kreya.test('status code is 200', () => {
expect(statusCode).to.eql(200);
});
While these examples are simple, you can create more realistic tests by referencing the gRPC or REST examples.
Viewing test results
Test results are displayed in the Tests tab, which provides a clear overview of passed and failed tests. The tab header shows the total number of passed tests, and detailed error messages are displayed for any failed tests.
Key features of the tests tab
- Real-time feedback: See test results immediately after running your scripts.
- Detailed error messages: Quickly identify and debug failed tests.
- Organized view: Easily navigate through multiple test cases.
- Snapshot diffing: When a snapshot changes, you can view a visual diff to easily see what has changed and decide whether to accept or reject the update.