Tests

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, mocks
  const functionDir = './build'
  const context = { /* required fields */ }
  const inputs = { /* manifest inputs */ }

  it('produces the expected intents', async () => {
    // 2. Execute the function
    const result = await runFunction(functionDir, context, { inputs, /* needed mocks */ })
    
    // 3. Check the function outputs
    expect(result.success).to.be.true
    expect(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.

Transfer

If the function creates a transfer intent:

Then, the test should be:

Swap

If the function creates a swap intent:

Then, the test should be:

Call

If the function creates a call intent:

Then, the test should be:

2.2.2. Logs

For example, if the function does:

Then, the test should be:

Last updated