我正在尝试使用测试库中的 renderHook。当我运行测试时,出现此错误,TypeError: Cannot read property 'hasOwnProperty' of undefined
包.json
"devDependencies": {
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.1",
"@testing-library/preact": "^2.0.0",
"@testing-library/react": "^10.4.5",
"@testing-library/react-hooks": "^3.4.1",
"babel-plugin-minify-constant-folding": "^0.5.0",
"babel-preset-react-app": "^9.0.1",
"cross-env": "^6.0.3",
"dot-prop": "^5.2.0",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"flow-bin": "0.142.0",
"jest": "^24.9.0",
"jest-sonar-reporter": "^2.0.0",
"kind-of": "^6.0.3",
"minimist": "^1.2.5",
"negotiator": "^0.6.2",
"prettier": "1.18.2",
"prettier-stylelint": "0.4.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-test-renderer": "^16.9.0",
"source-map-loader": "^0.2.4",
"stylelint": "12.0.0",
"wait-for-expect": "^3.0.2"
}
我的单元测试文件是
import { act, renderHook } from '@testing-library/react-hooks';
import { usePlayBeepingSoundAndBackButtonClick } from '../usePlayBeepingSoundAndBackButtonClick';
describe('usePlayBeepingSoundAndBackButtonClick', () => {
it('should call the callback onBackButtonClick', () => {
const mockCallback = jest.fn();
const { result } = renderHook(() =>
usePlayBeepingSoundAndBackButtonClick(mockCallback)
);
act(() => result.current());
expect(mockCallback).toHaveBeenCalled();
});
});
我运行单元测试时的错误消息是 TypeError: Cannot read property 'hasOwnProperty' of undefined
TypeError: Cannot read property 'hasOwnProperty' of undefined
> 19 | import { act, renderHook } from '@testing-library/react-hooks';
| ^
22 | import { usePlayBeepingSoundAndBackButtonClick } from '../usePlayBeepingSoundAndBackButtonClick';
at node_modules/.pnpm/react-test-renderer@16.9.0_react@16.13.1/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:153:27
at Object.<anonymous> (node_modules/.pnpm/react-test-renderer@16.9.0_react@16.13.1/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15959:5)
at Object.<anonymous> (node_modules/.pnpm/react-test-renderer@16.9.0_react@16.13.1/node_modules/react-test-renderer/index.js:6:20)
at Object.<anonymous> (node_modules/.pnpm/@testing-library/react-hooks@3.4.1_0d7f26cd33b086e6db0ec263aae150bb/node_modules/@testing-library/react-hooks/lib/pure.js:28:26)
at Object.<anonymous> (node_modules/.pnpm/@testing-library/react-hooks@3.4.1_0d7f26cd33b086e6db0ec263aae150bb/node_modules/@testing-library/react-hooks/lib/index.js:13:13)
at Object.<anonymous> (src/components/common/hooks/tests/usePlayBeepingSoundAndBackButtonClick.test.js:19:1)
```