8 – How to kernel test a custom formatter for an entity reference field type?

We have a custom field formatter for a string and an entity reference field type. While this works for testing the string field:

   $node = $this->createNode(('type' => 'page', 'field_id' => 'foo12345'));
   $build = $entity->field_id->view('default');
   $this->assertSame('Custom Label', $build('#title'));
   $this->assertSame('foo12345', $build(0)('#context')('value'));

The same does not work for an entity reference field type. $build is always empty:

    $node1 = $this->createNode(('type' => 'page', 'field_id' => 'foo12345'));
    $node2 = $this->createNode(('type' => 'page', 'field_ref_field' => ('target_id' => $node1->id())));
    $build = $node2->field_ref_field->view(('type' => 'my_plugin'));
    $this->assertSame('Custom Label', $build(0)('#title'));

I tried checking some of the core tests around reference field displays but came up short. Why does ->view() not return a build array (other than the cache metadata) for an entity reference field?

edit: it seems that in the test, this part of the formatter plugin returns nothing from ‘getEntitiesToView’ (but works on the actual site):

  public function viewElements(FieldItemListInterface $items, $langcode) : array {
    $elements = ();

    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
      $id = $entity->field_id->value;
      $uri = $entity->toUrl();
      $label = $this->formatLabel($id) . ' ' . $this->stripCharacters($id);

      $elements($delta) = (
        '#type' => 'link',
        '#title'=> $label,
        '#url'  => $uri,
        '#options' => $uri->getOptions(),

      $elements($delta)('#options') += ('attributes' => ());
      $elements($delta)('#cache')('tags') = $entity->getCacheTags();

    return $elements;