Issue with Mocking Agent for Testing - Input Too Short Error

Hello, community,

I’m working on an NPM library that simplifies store management for IC developers. As part of my development process, I’m trying to create a test suite using Jest and Jest Fetch Mock to mock agent responses. However, I’m encountering an error that I can’t seem to resolve.

Problem Description:

I have created a Jest test suite to test the functionality of my library. In this suite, I’m mocking responses from the agent using Jest Fetch Mock. The test suite initializes an actor, calls query and update methods, and checks the expected behavior.

However, when I run the test suite, I encounter the following error:

CreateActor
    ✓ should initialize the actor (61 ms)
    ✓ should call the query method (13 ms)
    ✕ should call the update method (9 ms)

  ● CreateActor › should call the update method

    Input too short

      at Uint8ArrayDecoder._decode (../../node_modules/borc/src/decoder.js:549:13)
      at Uint8ArrayDecoder.decodeFirst (../../node_modules/borc/src/decoder.js:577:10)
      at Object.decode (../../node_modules/@dfinity/agent/src/cbor.ts:131:18)
      at new Certificate (../../node_modules/@dfinity/agent/src/certificate.ts:171:22)
      at Function.create (../../node_modules/@dfinity/agent/src/certificate.ts:152:18)
      at pollForResponse (../../node_modules/@dfinity/agent/src/polling/index.ts:38:34)
      at caller (../../node_modules/@dfinity/agent/src/actor.ts:440:29)

Code:

Here is the complete test suite code:

import { Cbor } from "@dfinity/agent"
import { IDL } from "@dfinity/candid"
import fetchMock from "jest-fetch-mock"
import createICStoreAndActions from "../src"
import { createActor } from "./candid/hello"

fetchMock.enableMocks()

const canisterDecodedReturnValue = "Hello, World!"
const expectedReplyArg = IDL.encode([IDL.Text], [canisterDecodedReturnValue])

fetchMock.mockResponse(async (req) => {
  console.log("mockResponse", req.url.split("/").findLast((_) => _) ?? "")

  if (req.url.endsWith("/call")) {
    return Promise.resolve({
      status: 200,
    })
  }

  const responseObj = {
    status: "replied",
    reply: {
      arg: expectedReplyArg,
    },
    root_key: [
      48, 129, 130, 48, 29, 6, 13, 43, 6, 1, 4, 1, 130, 220, 124, 5, 3, 1, 2, 1,
      6, 12, 43, 6, 1, 4, 1, 130, 220, 124, 5, 3, 2, 1, 3, 97, 0, 152, 245, 4,
      39, 146, 174, 234, 225, 244, 127, 143, 22, 30, 244, 96, 2, 185, 106, 236,
      194, 117, 132, 255, 155, 140, 185, 7, 232, 134, 106, 159, 201, 151, 124,
      250, 187, 134, 24, 222, 199, 14, 102, 155, 207, 175, 235, 0, 227, 1, 25,
      5, 48, 61, 231, 72, 146, 168, 216, 60, 223, 174, 138, 180, 149, 247, 3,
      97, 218, 226, 71, 68, 160, 168, 2, 86, 197, 60, 84, 86, 174, 6, 208, 158,
      57, 110, 122, 8, 222, 41, 146, 247, 102, 75, 37, 145, 253,
    ],
  }

  return Promise.resolve({
    status: 200,
    body: Cbor.encode(responseObj),
  })
})

describe("CreateActor", () => {
  const [{ getState }, { initialize, call }] = createICStoreAndActions(() =>
    createActor("bd3sg-teaaa-aaaaa-qaaba-cai")
  )

  it("should initialize the actor", () => {
    initialize()

    expect(getState().initialized).toEqual(true)
  })

  it("should call the query method", async () => {
    const reply = await call("greet", "World")

    expect(reply).toEqual(canisterDecodedReturnValue)
    expect(getState().data).toEqual(canisterDecodedReturnValue)
  })

  it("should call the update method", async () => {
    const replyUpdate = await call("greet_update", "World")

    expect(replyUpdate).toEqual(canisterDecodedReturnValue)
    expect(getState().data).toEqual(canisterDecodedReturnValue)
  })
})

Question:

I’m struggling to identify the root cause of this “Input too short” error. I suspect it might be related to how I’m mocking responses or encoding/decoding data, but I can’t pinpoint the issue.

Can someone please review my test suite code and help me understand what might be causing this error and how I can fix it? Any insights or suggestions would be greatly appreciated.

Thank you in advance for your assistance!

1 Like

Hey @kpeacock, Thank you for the awesome Agent library. can you take a look at this?

1 Like

Mocking responses is a real pain (speaking from extensive experience). I’d recommend e2e tests for actual calls to canisters, and use unit tests for logic that can be independently tested

1 Like

Agree :sweat_smile:

I’ll do e2e then, thanks for your response.