Jest dom Get-TypeError:expect(…)。即使经过正确的设置,toBeInTheDocument也不是函数

Jest dom Get-TypeError:expect(…)。即使经过正确的设置,toBeInTheDocument也不是函数,jest-dom,Jest Dom,我使用Create-React应用程序,并已在src/setupTests.js上声明: import'@testinglibrary/jest-dom'; 从“酶”导入{configure}; 从'enzyme-Adapter-react-16'导入适配器; 配置({adapter:newadapter()}); 但每次我在测试文件上使用expect(anything).toBeInTheDocument(),运行测试时,我得到: TypeError: expect(...).toBeInT

我使用Create-React应用程序,并已在src/setupTests.js上声明:

import'@testinglibrary/jest-dom';
从“酶”导入{configure};
从'enzyme-Adapter-react-16'导入适配器;
配置({adapter:newadapter()});
但每次我在测试文件上使用
expect(anything).toBeInTheDocument()
,运行测试时,我得到:

TypeError: expect(...).toBeInTheDocument is not a function
为了确保setupTests.js实际运行,我尝试在测试文件上使用Ezyme shallow,它可以正常工作。那么jest dom到底有什么问题?如何解决它呢?

解决方法是:

import '@testing-library/jest-dom/extend-expect';

在src/setupTests.js上,添加
jest.config.js

module.exports = {
  ...,
  "setupFilesAfterEnv": [
    "<rootDir>/jest.setup.js"
  ]
}
这样,您就不必在每个测试文件中导入jest dom

import '@testing-library/jest-dom'