Open fjpedrosa opened 2 months ago
I found that the problem seems not to be related with userEvent / fireEvent, but with having getByRole
and getByText
being used in the same test. For instance this will work:
it('is changing the state when clicked', async () => {
const handleChange = mock();
const { getByRole } = render(
<Toggle label="Toggle test" onChange={handleChange} />
);
const toggle = getByRole('checkbox');
expect(toggle).not.toBeChecked();
await userEvent.click(toggle);
expect(toggle).toBeChecked();
});
This will pass as well:
it('is changing the state when clicked', async () => {
const handleChange = mock();
const { getByRole, getByText } = render(
<Toggle label="Toggle test" onChange={handleChange} />
);
const toggle = getByRole('checkbox');
const label = getByText('Toggle test');
expect(toggle).not.toBeChecked();
await userEvent.click(toggle);
expect(toggle).toBeChecked();
});
But if the click is executed in label instead of toggle:
it('is changing the state when clicked', async () => {
const handleChange = mock();
const { getByRole, getByText } = render(
<Toggle label="Toggle test" onChange={handleChange} />
);
const toggle = getByRole('checkbox');
const label = getByText('Toggle test');
expect(toggle).not.toBeChecked();
await userEvent.click(label);
expect(toggle).toBeChecked();
});
72 | const toggle = getByRole('checkbox');
73 | const label = getByText('Toggle test');
74 |
75 | expect(toggle).not.toBeChecked();
76 | await userEvent.click(label);
77 | expect(toggle).toBeChecked();
^
error: expect(received).toBeChecked()
expect(received)..toBeChecked(expected)
Expected: ""
Received: "element"
Received element is not checked:
HTMLInputElement {
[Symbol(listeners)]: {},
[Symbol(listenerOptions)]: {},
[Symbol(isConnected)]: false,
[Symbol(parentNode)]: null,
[Symbol(rootNode)]: null,
[Symbol(formNode)]: null,
[Symbol(selectNode)]: null,
[Symbol(textAreaNode)]: null,
[Symbol(observers)]: [],
What version of Bun is running?
1.1.3+2615dc742
What platform is your computer?
Darwin 23.4.0 arm64 arm
What steps can reproduce the bug?
Using this test code:
when running
bun test
, he console throws an error message.What is the expected behavior?
Success execution of the test: ✓ Toggle > Rendering and Interaction > is triggering a click on the input when the label is clicked
What do you see instead?
Additional information
When commenting the userEvent and subsequent line, the test is passed:
✓ Toggle > Rendering and Interaction > is triggering a click on the input when the label is clicked
Using:
"@happy-dom/global-registrator": "^14.7.1"