Skip to content

Textfield errorMessage not rendered in tests #16240

Answered by ecraig12345
mccuna asked this question in Q&A
Discussion options

You must be logged in to vote

TextField's error message currently uses delayed rendering in an attempt to ensure that the error is read by screen readers on initial render. But that also causes the error message to initially not be rendered in tests.

To work around this, you'll need to use fake timers in your test, and advance the timers before looking for the message. This unfortunately can't be demoed in codesandbox due to its lack of full jest support, but here's the code:

beforeEach(() => {
  jest.useFakeTimers();
});
afterEach(() => {
  jest.runOnlyPendingTimers();
  jest.useRealTimers();
});

test('dummy test', () => {
  const errorMsgText = 'Error Message';
  render(<TextField defaultValue="Default Value" error…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mccuna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #16240 on December 16, 2020 21:52.