React 测试库 - 全局配置

React Testing Library - Global configuration

我需要覆盖 React 测试库中的默认 testID 属性。使用 Jest 作为测试框架。

为此,我在单个测试文件级别遵循了 here 给出的步骤。

有什么方法可以在全局(项目)级别进行此配置,以便项目中的所有测试文件都遵循此类自定义配置,而无需在测试文件级别显式定义自定义配置?

如果使用 jest,您可以在文件中添加以下内容

import { configure } from 'react-testing-library';

configure({
  testIdAttribute: 'data-my-test-id',
});

并将该文件包含在 setupFiles 属性

中的 jest.config.js 文件中
setupFiles: [
  '<rootDir>/path-to-your-configuration-file.js',
]

https://testing-library.com/docs/dom-testing-library/api-configuration/#introduction

import {configure} from '@testing-library/dom'

configure({
  testIdAttribute: 'data-my-test-id',
  
})