Objects.search().all() returns undefined in tests

We are running into unexpected behaviour with a user while trying to author a simple TypeScript function.

A minimal repro looks something like this:

@Function()
public testFunction(): Long {
    const existingObjects = Objects.search()
        .myTestObject()
        .all();
    return existingObjects.length;
}

The return type of .all() should be MyTestObject[], so we expect this function to always return a number, either 0 or whatever number of objects we have access to.

In live preview, this function works and returns e.g. “6”. However, in the unit tests in this same repo, we are for some reason getting “undefined” back from the search, and tests fail with this error:

     TypeError: Cannot read properties of undefined (reading 'length')

       9 |             .all();
      10 |
    > 11 |         return existingObjects.length;
         |                                ^
      12 |     }
      13 | }
      14 |

Why is this happening? Shouldn’t the result be “0”, or some other number?

In the unit testing environment, you need to stub object search operations because the test environment is sandboxed from the network. These docs explain how to do it: https://www.palantir.com/docs/foundry/functions/unit-test-object-searches/

For example, to make the above code return “0”, add this setup to your tests:

whenObjectSet(
        Objects.search()
        .myTestObject()
        .all()
    ).thenReturn([]);