Skip to content

getByLabelText returns an old reference in 9.0.0-beta.1 #67

Closed
@tonivj5

Description

@tonivj5

Hi @timdeschryver, after a long time (~1 week) searching what was breaking my tests. I have discovered that the upgrade to beta release was the culprit.

It seems that getByLabelText is using an old reference of fixture the or nativeElement. I have created a repro where you can see the bug (it has cost a lot of hours to reproduce it 😅)

Repository with the test, use the branch bug/getByLabelText and run the command yarn test bug- to launch it (it should broke 💥).

import { Component } from '@angular/core';
import { render } from '../src/public_api';

@Component({
  template: `
    <div>
      <!-- if remove for="name" no error happens -->
      <label for="name">
        <input type="checkbox" id="name" data-testid="checkbox" />
        TEST
      </label>
    </div>
  `,
})
export class BugGetByLabelTextComponent {}

it('first step to reproduce the bug: skip this test to avoid the error or remove the for attribute of label', async () => {
  expect(await render(BugGetByLabelTextComponent)).toBeDefined();
});

it('second step: bug happens :`(', async () => {
  const { getByLabelText, getByTestId } = await render(BugGetByLabelTextComponent);

  const checkboxByTestId = getByTestId('checkbox');
  const checkboxByLabelTest = getByLabelText('TEST');

  expect(checkboxByTestId).toBe(checkboxByLabelTest);
});

If you run this test in master, it success. If you run this test in beta it breaks, because checkboxByTestId and checkboxByLabelTest aren't equal, they are two different objects (input html objects). But, if you skip the first test (it.skip) the test is successfully. And, other way to pass the test is removing the for="name" attribute (I think it happens because, the input is get using label.control if for is present and linked). This error is a big problem because if, for example, I use click(checkboxByLabelTest), it doesn't run change detection, test fail and it's difficult to determine what is happening.

I think it's related with the update of @testing-library/dom, I have tried to downgrade it to master's version, but it continues failing. Maybe, this bug should be reported at @testing-library/dom instead of here, but I am not sure, sorry if it so.

Thanks for your work in this library, I'm so exciting using it in my project! 👍

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions