jestjsreact-testing-library

React Testing Library - Global configuration


I need to override default testID attribute in React Testing Library. Using Jest as a testing framework.

For this, I have followed the steps given here at individual test file level.

Is there any way to have this configuration at global (project) level, so that all the test files in the project will adhere to such custom configuration and will not have need to define custom configuration explicitly at test file level?


Solution

  • If using jest you can add the following in file

    import { configure } from 'react-testing-library';
    
    configure({
      testIdAttribute: 'data-my-test-id',
    });
    

    and include that file in the jest.config.js file in the setupFiles property

    setupFiles: [
      '<rootDir>/path-to-your-configuration-file.js',
    ]