question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'getByRole' doesn't find a table with a name specified by <caption>

See original GitHub issue

Hello everyone and thanks for the work done. I’m having what i consider an error when trying to locate a <table> with a <caption> inside.

  • @testing-library/dom version: “6.16.0”
  • Testing Framework and version: – @testing-library/react: “9.5.0”; – @types/jest: “24.9.1”; – with env jest-environment-jsdom-sixteen: “^1.0.3”; – node: v12.16.3

Relevant code or config:

A sample test:

describe('Some problem', () => {
  it('doens\'t considers a <caption> as <table>\'s "name"', () => {
    const { getByRole } = render(
      <table>
        <caption>Monthly savings</caption>
        <thead>
          <tr>
            <th>Month</th>
            <th>Savings</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>January</td>
            <td>$100</td>
          </tr>
        </tbody>
      </table>
    );
    expect(getByRole('table', { name: 'Monthly savings' }))
      .toBeTruthy();
  });
});
my 'package.json' file
{
  "name": "apidesk",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.28",
    "@fortawesome/free-brands-svg-icons": "^5.13.0",
    "@fortawesome/free-regular-svg-icons": "^5.13.0",
    "@fortawesome/free-solid-svg-icons": "^5.13.0",
    "@fortawesome/react-fontawesome": "^0.1.9",
    "@types/react-dom": "^16.9.5",
    "airbnb-prop-types": "^2.15.0",
    "array-move": "^2.2.1",
    "bootstrap": "^4.4.1",
    "keycode-js": "^2.0.3",
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-bootstrap": "^1.0.0",
    "react-collapsible": "^2.7.0",
    "react-country-flag": "^2.0.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.0",
    "react-scripts": "3.4.0",
    "react-sortable-hoc": "^1.11.0",
    "react-tabs": "^3.1.0",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "reselect": "^4.0.0",
    "simplebar": "^5.1.0",
    "simplebar-react": "^2.1.0",
    "typescript": "^3.7.5",
    "uniqid": "^5.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --watchAll --env=jest-environment-jsdom-sixteen",
    "eject": "react-scripts eject",
    "pretest": "concurrently 'npm run eslint' 'npm run stylelint'",
    "eslint": "eslint './src/**/*.{ts,tsx,js,jsx}' './src/*.{ts,tsx,js,jsx}'",
    "stylelint": "stylelint './src/**/*.css'"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "@types/jest": "^24.9.1",
    "@types/react": "^16.9.27",
    "@typescript-eslint/eslint-plugin": "^2.26.0",
    "@typescript-eslint/parser": "^2.26.0",
    "babel-eslint": "^10.0.3",
    "concurrently": "^5.1.0",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2",
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.1.0",
    "eslint-config-react": "^1.1.7",
    "eslint-config-react-app": "^5.2.1",
    "eslint-plugin-flowtype": "^3.13.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "^1.7.0",
    "jest-environment-jsdom-sixteen": "^1.0.3",
    "stylelint": "^13.3.1",
    "stylelint-config-standard": "^16.0.0"
  },
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx}",
      "!src/index.jsx",
      "!src/serviceWorker.js",
      "!src/**/index.js",
      "!src/**/*Shape.js"
    ]
  }
}

What I did:

With the configuration and code above, i ran npm test.

What happened:

As output for this specific test, i had:

Unable to find an accessible element with the role "table" and name "Monthly savings"

    Here are the accessible roles:

...

--------------------------------------------------
      table:

      Name "":
      <table />

--------------------------------------------------

...

Reproduction:

This repository reproduces the problem (forked from dom-testing-library-template).

Problem description:

As described in WAI-ARIA Authoring Practices 1.1:

If the table element does not have aria-label or aria-labelledby, then the caption will be used as the accessible name.

Suggested solution:

‘getByRole’ should consider <caption> as name if it exists inside table and “the table element does not have aria-label or aria-labelledby”.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
eps1loncommented, May 18, 2020

@Tolsee I’ve put up a fix for that since a similar fix was merged yesterday.

1reaction
Tolseecommented, May 15, 2020

@eps1lon Thanks for the insights, I will try to work on the dom-accessibility-api ASAP. I will let you know If I ran into any issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ByRole | Testing Library
If you only query for a single element with getByText('The name') it's oftentimes better to use getByRole(expectedRole, { name: 'The name' }) ....
Read more >
Unable to use getByRole on listitem with a specific name - RTL
For your specific query I'd suggest getAllByRole('listitem').find(listitem => listitem.textContent === 'Pending task') . Share.
Read more >
ARIA: table role - Accessibility - MDN Web Docs - Mozilla
The table value of the ARIA role attribute identifies the element ... The table caption can be defined via aria-labelledby or aria-label ....
Read more >
React testing library getbyrole example
If the table element does not have aria-label or aria-labelledby, then the caption will be used as the accessible name. 2021-10-22 · Get...
Read more >
React testing library getbyrole example
Instead of searching by class name, we find elements by role, label, display text, etc. ... An element doesn't have multiple roles in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found