我已经到处寻找答案,并且可能尝试了 99% 的事情,所以我决定创建一个自己的线程,以便其他人可以查看我目前拥有的内容,看看他们是否能发现问题。
我对 Jest 测试非常陌生,并决定尝试在我们的代码库中实现它。我使用这个指南来确保我所做的一切都是完美的,但仍然会出现这个错误 A Practical Guide To Testing React Applications With Jest
我正在测试一个简单的功能组件,它使用 react-hook-form 在页面上生成一个表单,然后通过 redux 调用将完成的表单发送到我们的后端
我已将 setupTests.js 文件设置为:
import '@testing-library/jest-dom'
import { configure } from "enzyme"
import Adapter from "enzyme-adapter-react-16";
import '@testing-library/jest-dom/extend-expect';
configure({ adapter: new Adapter() });
将我的 package.json 测试命令更新为
"test": "react-scripts test --env=jsdom --setupFiles ./src/setupTests.js"
这是我试图通过一个简单的测试运行的测试规范:
import React from 'react';
import { render as rtlRender, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import store from '../../../store';
import AddNewProperty from './AddNewProperty';
configure({ adapter: new Adapter() });
const render = component => rtlRender(
<Provider store={store()}>
{component}
</Provider>
)
describe('Add New Property', () => {
test('component redners successfully', () => {
render(<AddNewProperty />)
// expect(screen.getByText('Apartment Number')).toBeInTheDocument();
})
});
这是屏幕上为我返回的错误:
FAIL src/components/Forms/Agency/AddNewProperty.spec.js
● Test suite failed to run
ReferenceError: expect is not defined
3 | import Adapter from "enzyme-adapter-react-16";
4 | import '@testing-library/jest-dom/extend-expect';
> 5 | configure({ adapter: new Adapter() });
| ^
at Object.<anonymous> (node_modules/@testing-library/jest-dom/dist/extend-expect.js:9:1)
at Object.<anonymous> (node_modules/@testing-library/jest-dom/dist/index.js:3:1)
at Object.<anonymous> (src/setupTests.js:5:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.167s
Ran all test suites related to changed files.
Watch Usage: Press w to show more.
我也安装了最新版本的所有软件包