The Mimic Protocol Test Library (@mimicprotocol/test-ts) provides tools for simulating function execution to validate expected function behavior under different scenarios.
1. Getting Started
1.1. Basic test structure
Every test follows this structure:
import{runFunction,/* types */}from'@mimicprotocol/test-ts'describe('Function',()=>{ // 1. Define context, inputs, mocksconstfunctionDir='./build'constcontext={/* required fields */}constinputs={/* manifest inputs */}it('produces the expected intents',async()=>{ // 2. Execute the functionconstresult=awaitrunFunction(functionDir,context,{inputs,/* needed mocks */}) // 3. Check the function outputsexpect(result.success).to.be.trueexpect(result.intents).to.have.lengthOf(N)})})
1.2. Project setup
Create a basic function project:
2. Function Runner Reference
This section describes the parameters and outputs of the runFunction function.
2.1. Parameters
2.1.1. Function directory
The directory where the compiled function .wasm is located.
2.1.2. Context
The context includes the fields needed during function execution.
2.1.3. Inputs
The values for the inputs defined in the manifest. For example, if the manifest declares:
Then, the inputs may be:
2.1.4. Prices mock
The responses for the price queries made in the function.
For example, if the function does:
Then, the prices mock may be:
2.1.5. Relevant tokens mock
The responses for the relevant tokens queries made in the function.
For example, if the function does:
Then, the relevant tokens mock may be:
2.1.6. Contract calls mock
The responses for the contract calls made in the function. Only for read functions, i.e., those that are not intended to generate intents.
Note: Token decimals and symbol are sometimes queried behind the scenes and need mocks in those cases.
For example, if the function does:
Then, the calls mock may be:
2.1.7. Subgraph queries mock
The responses for the subgraph queries made in the function.
For example, if the function does:
Then, the subgraph queries mock may be:
2.2. Output
The runFunction function returns an object containing the following fields:
success - Boolean. True if the execution ended properly, or false if it had an error.
timestamp - Number. Execution timestamp in milliseconds.
fuelUsed - Number. Amount of fuel used during the execution.
intents - Array of intents produced by the execution.
logs - Array of logs produced by the execution. It may include error logs.
2.2.1. Intents
The fields depend on the type of intents produced by the execution.
# Initialize a new Mimic project
npx @mimicprotocol/cli init my-automation-function && cd my-automation-function
# Test your function
yarn mimic test
const functionDir = './build'
import { Context } from '@mimicprotocol/test-ts'
const context: Context = {
user: '0xAddress',
settlers: [{ address: '0xAddress', chainId: 10 }], // One per chain used in the function
timestamp: Date.now(), // number (in milliseconds)
}