diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js
index c01c0763..1fa122b3 100644
--- a/src/__tests__/element-queries.js
+++ b/src/__tests__/element-queries.js
@@ -88,7 +88,7 @@ test('get throws a useful error message', () => {
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with the role "LucyRicardo"
-Here are the available roles:
+There are no available roles.
diff --git a/src/__tests__/role.js b/src/__tests__/role.js
index 77aa9ee1..30e6b259 100644
--- a/src/__tests__/role.js
+++ b/src/__tests__/role.js
@@ -20,3 +20,16 @@ Here are the available roles:
"
`)
})
+
+test('logs error when there are no available roles', () => {
+ const {getByRole} = render('')
+ expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
+"Unable to find an element with the role "article"
+
+There are no available roles.
+
+"
+`)
+})
diff --git a/src/queries/role.js b/src/queries/role.js
index 9ce1fccc..6792b5ab 100644
--- a/src/queries/role.js
+++ b/src/queries/role.js
@@ -26,16 +26,26 @@ function queryAllByRole(
const getMultipleError = (c, role) =>
`Found multiple elements with the role "${role}"`
-const getMissingError = (container, role) =>
- `
-Unable to find an element with the role "${role}"
+const getMissingError = (container, role) => {
+ const roles = prettyRoles(container)
+ let roleMessage
+
+ if (roles.length === 0) {
+ roleMessage = 'There are no available roles.'
+ } else {
+ roleMessage = `
Here are the available roles:
- ${prettyRoles(container)
- .replace(/\n/g, '\n ')
- .replace(/\n\s\s\n/g, '\n\n')}
+ ${roles.replace(/\n/g, '\n ').replace(/\n\s\s\n/g, '\n\n')}
`.trim()
+ }
+
+ return `
+Unable to find an element with the role "${role}"
+
+${roleMessage}`.trim()
+}
const [
queryByRole,