我使用 Create React App 并已经在 src/setupTests.js 上声明了这一点:
import '@testing-library/jest-dom';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)
但每次我expect(anything).toBeInTheDocument()在测试文件上使用时,运行测试时我都会得到:
TypeError: expect(...).toBeInTheDocument is not a function
Run Code Online (Sandbox Code Playgroud)
为了确保 setupTests.js 实际运行,我尝试在测试文件上使用酶浅层并且它有效。那么 jest-dom 到底存在什么问题以及如何解决呢?
Ino*_*ani 22
解决方法:
import '@testing-library/jest-dom/extend-expect';
Run Code Online (Sandbox Code Playgroud)
在 src/setupTests.js 上
use*_*695 12
添加到您的jest.config.js
module.exports = {
...,
"setupFilesAfterEnv": [
"<rootDir>/jest.setup.js"
]
}
Run Code Online (Sandbox Code Playgroud)
jest.setup.js并用内容进行创作
import '@testing-library/jest-dom'
Run Code Online (Sandbox Code Playgroud)
这样你就不必在每个测试文件中导入 jest-dom
| 归档时间: |
|
| 查看次数: |
13370 次 |
| 最近记录: |