I tried to mock the generator to accomplish this but couldn't get it to work.
I would have provided an codesandbox example but the jest.mock is not yet supported.
import { render, screen } from "@testing-library/react";
import * as jest from "jest";
import { EuiFormRow, EuiFlexItem, EuiTextArea } from "@elastic/eui";
it("Renders OK", async () => {
jest.mock("@elastic/eui", () => ({
htmlIdGenerator: jest.fn(() => () => "constant_id")
}));
render(
<EuiFlexItem>
<EuiFormRow>
<EuiTextArea label="ss"></EuiTextArea>
</EuiFormRow>
</EuiFlexItem>
);
screen.debug(); // you can see on the console the generated ids are still random
// expect(component).toMatchSnapshot();
});
Try this instead:
jest.mock("@elastic/eui/lib/services/accessibility/html_id_generator", () => ({
htmlIdGenerator: () => () => "constant_id"
}));
10x, I already tried it, also not working.
Created an new project with only the necessary dependencies, the snippet provided by @thompsongl works for me. I did adjust:
import jest from and import * as jest from ran for meOne other thing you can try is changing the lib directory in the mock to es or testenv. The right target depends on the rest of your environment configuration.

Awesome! The issue was I had the mock action inside my test.
When moving it outside of it (also outside of any "descibe" block) and using your full path mock action, it works.
any idea why it should be outside? anyway, you can close it thanks!
Jest moves the jest.mock calls above the imports (which are transpiled to require), as it needs the mocking information prior to resolving the other files. When jest.mock is not top-level, it isn't hoisted above the imports. Sometimes that works, but is usually not the desired functionality.
Most helpful comment
Created an new project with only the necessary dependencies, the snippet provided by @thompsongl works for me. I did adjust:
import jest fromandimport * as jest fromran for meOne other thing you can try is changing the
libdirectory in the mock toesortestenv. The right target depends on the rest of your environment configuration.