有没有办法为 Jest 中的所有测试文件预定义通用导入?

Is there a way to predefine common imports for all test files in Jest?

我正在使用 Jest 来测试 React 应用程序。对于每次测试中需要的一些常见 libraries/functionalities,我一直重复相同的导入语句。我想知道是否有任何方法可以告诉 Jest 在每次测试之前导入这些库。

我通过在 setupTests.js 中导入必要的库来实现它,如果存在,默认情况下会在每个测试文件之前调用。为了使导入的库可用于测试,我必须执行以下操作:

假设我需要在每个测试中导入 enzyme 中的 mount

// setupTests.js
import { mount } from 'enzyme';

global.mount = mount;

这样,我可以直接在任何测试文件中使用mount,而不需要导入它。