Mock Dependencies Manually
HyperTest provides a way for you to manually mock certain dependencies that are not instrumented automatically. Allows you to capture mocks with a readable input, which will help you better understand the mocks on the dashboard.
Please check the list of automatically instrumented packages, before proceeding with manual mocks.
These mocks also come in handy when you want to reduce false positives in the test reports caused by a utility that returns arbitrary values e.g., timestamps, and UUIDs.
Let's look at an example where manual mocks can be utilized.
Below is an API route that generates PROMO code for a user.
If we record this API interaction and run a Test then HyperTest will report a value-modified error, but this is a false positive and it's not a real change that was made in the logic.
To eliminate this recurrent issue we can manually mock the generation of the Promo code. When we re-record this API interaction we will also have the exact value of the Promo code that was generated and the same will be used in the Test.
To create a manual mock you need to follow these steps:
Add annotation: Add an annotation (@HtManualMock) above the method you want to manually mock. This annotation accepts an argument called configClass.
Create a config class: Create a config class for this mock. This class must implement HtManualMockConfig interface. You have to implement 2 mandatory methods and 1 optional method:
generateIdentifier(Object ...args): It accepts the same arguments as
originalFn
. It should return a string which will be your unique identifier (An identifier is needed to pick the right mock during REPLAY, if you are making the same kind of function calls then consider adding a number to the identifier).getFunctionType(): It should return an ENUM (EnumManager.FunctionTypesEnum). It tells us that that method you are trying to manually mock is a SYNC/ASYNC/CALLBACK method.
normalizeArguments(Object... args): This method is optional and normalizes or transforms the input arguments. It accepts the same arguments as
originalFn
and returns an object with normalized input values. Return value of this function will be used as readableInput and would be used for display on dashboard.
If we run a test with the manual mock in place it passes.
Last updated