Skip to content

Commit f58e2d7

Browse files
committed
Add toHaveAccessibleErrorMessage to @testing-library/jest-dom Types
Created in response to (and thus depends on): - testing-library/jest-dom#503
1 parent b0312dc commit f58e2d7

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

types/testing-library__jest-dom/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ declare global {
1717
}
1818
}
1919

20-
declare module "expect" {
20+
declare module 'expect' {
2121
interface Matchers<R = void, T = unknown> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
2222
}

types/testing-library__jest-dom/matchers.d.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,47 @@ declare namespace matchers {
503503
* [testing-library/jest-dom#tohaveaccessibledescription](https://github.com/testing-library/jest-dom#tohaveaccessibledescription)
504504
*/
505505
toHaveAccessibleDescription(text?: string | RegExp | E): R;
506+
507+
/**
508+
* @description
509+
* This allows you to assert that an element has the expected
510+
* [accessible error message](https://w3c.github.io/aria/#aria-errormessage).
511+
*
512+
* You can pass the exact string of the expected accessible error message.
513+
* Alternatively, you can perform a partial match by passing a regular expression
514+
* or by using either
515+
* [expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring)
516+
* or [expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp).
517+
*
518+
* @example
519+
* <input aria-label="Has Error" aria-invalid="true" aria-errormessage="error-message" />
520+
* <div id="error-message" role="alert">This field is invalid</div>
521+
*
522+
* <input aria-label="No Error Attributes" />
523+
* <input aria-label="Not Invalid" aria-invalid="false" aria-errormessage="error-message" />
524+
*
525+
* // Inputs with Valid Error Messages
526+
* expect(getByRole('textbox', {name: 'Has Error'})).toHaveAccessibleErrorMessage()
527+
* expect(getByRole('textbox', {name: 'Has Error'})).toHaveAccessibleErrorMessage('This field is invalid')
528+
* expect(getByRole('textbox', {name: 'Has Error'})).toHaveAccessibleErrorMessage(/invalid/i)
529+
* expect(
530+
* getByRole('textbox', {name: 'Has Error'}),
531+
* ).not.toHaveAccessibleErrorMessage('This field is absolutely correct!')
532+
*
533+
* // Inputs without Valid Error Messages
534+
* expect(
535+
* getByRole('textbox', {name: 'No Error Attributes'}),
536+
* ).not.toHaveAccessibleErrorMessage()
537+
*
538+
* expect(
539+
* getByRole('textbox', {name: 'Not Invalid'}),
540+
* ).not.toHaveAccessibleErrorMessage()
541+
*
542+
* @see
543+
* [testing-library/jest-dom#tohaveaccessibleerrormessage](https://github.com/testing-library/jest-dom#tohaveaccessibleerrormessage)
544+
*/
545+
toHaveAccessibleErrorMessage(text?: string | RegExp | E): R;
546+
506547
/**
507548
* @description
508549
* This allows to assert that an element has the expected [accessible name](https://w3c.github.io/accname/).
@@ -573,8 +614,10 @@ declare namespace matchers {
573614
*/
574615
toBePartiallyChecked(): R;
575616
/**
576-
* @description
617+
* @deprecated
618+
* since v5.17.0
577619
*
620+
* @description
578621
* Check whether the given element has an [ARIA error message](https://www.w3.org/TR/wai-aria/#aria-errormessage) or not.
579622
*
580623
* Use the `aria-errormessage` attribute to reference another element that contains

types/testing-library__jest-dom/test/testing-library__jest-dom-extend-matchers-tests.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ customExpect(element).toHaveAccessibleDescription('some description');
5656
customExpect(element).toHaveAccessibleDescription(/some description/);
5757
customExpect(element).toHaveAccessibleDescription(expect.stringContaining('partial'));
5858
customExpect(element).toHaveAccessibleDescription();
59+
60+
customExpect(element).toHaveAccessibleErrorMessage();
61+
customExpect(element).toHaveAccessibleErrorMessage('Invalid time: the time must be between 9:00 AM and 5:00 PM');
62+
customExpect(element).toHaveAccessibleErrorMessage(/invalid time/i);
63+
customExpect(element).toHaveAccessibleErrorMessage(expect.stringContaining('Invalid time'));
64+
5965
customExpect(element).toHaveAccessibleName('a label');
6066
customExpect(element).toHaveAccessibleName(/a label/);
6167
customExpect(element).toHaveAccessibleName(expect.stringContaining('partial label'));
@@ -105,6 +111,11 @@ customExpect(element).not.toHaveDescription('some description');
105111
customExpect(element).not.toHaveDescription();
106112
customExpect(element).not.toHaveAccessibleDescription('some description');
107113
customExpect(element).not.toHaveAccessibleDescription();
114+
115+
customExpect(element).not.toHaveAccessibleErrorMessage();
116+
customExpect(element).not.toHaveAccessibleErrorMessage('There is no date');
117+
customExpect(element).not.toHaveAccessibleErrorMessage(/everything is valid/i);
118+
108119
customExpect(element).not.toHaveAccessibleName('a label');
109120
customExpect(element).not.toHaveAccessibleName();
110121
customExpect(element).not.toBePartiallyChecked();

0 commit comments

Comments
 (0)