【发布时间】:2018-09-21 22:10:57
【问题描述】:
我看过非常相似的帖子: Using Jest to test a Link from react-router v4 但是我在尝试使用 CRA 和 React Router 时遇到了各种错误。
如果测试脚本是"test": "react-scripts test --env=jsdom",我会收到此错误,阻止测试运行:
2017-06-28 14:14 node[6612] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
Error: Error watching file for changes: EMFILE
at exports._errnoException (util.js:1018:11)
at FSEvent.FSWatcher._handle.onchange (fs.js:1420:11)
我找到的一个解决方案是安装/重新安装一个名为 watchman 的库,但我无法让它工作。 (https://github.com/facebook/react-native/issues/10028) 但尝试安装 watchman 也会抛出错误。
我找到的另一个解决方案 (https://github.com/jest-community/vscode-jest/issues/125) 是将测试脚本更改为 "test": "jest",然后我收到此错误:
src/App.test.js: Unexpected token (7:18)
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
> 7 | ReactDOM.render(<App />, div);
| ^
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.646s
Ran all test suites.
npm ERR! Test failed. See above for more details.
在回溯我的提交历史以查找测试套件最初出现问题的位置后,我发现了一组不同的错误。 第一个错误发生在安装 react-router-dom 并将这个衬垫添加到 App.js 之后
<Link to="/">Scratch</Link>
我收到此错误:
console.error node_modules/fbjs/lib/warning.js:33
Warning: Failed context type: The context `router` is marked as required in`Link`, but its value is `undefined`.
in Link (at App.js:13)
in NavbarBrand (at App.js:12)
in div (created by NavbarHeader)
in NavbarHeader (at App.js:11)
in div (created by Grid)
in Grid (created by Navbar)
in nav (created by Navbar)
in Navbar (created by Uncontrolled(Navbar))
in Uncontrolled(Navbar) (at App.js:10)
in div (at App.js:9)
in App (at App.test.js:7)
console.error node_modules/react-dom/cjs/react-dom.development.js:9627
The above error occurred in the <Link> component:
in Link (at App.js:13)
in NavbarBrand (at App.js:12)
in div (created by NavbarHeader)
in NavbarHeader (at App.js:11)
in div (created by Grid)
in Grid (created by Navbar)
in nav (created by Navbar)
in Navbar (created by Uncontrolled(Navbar))
in Uncontrolled(Navbar) (at App.js:10)
in div (at App.js:9)
in App (at App.test.js:7)
我尝试的一切似乎都产生了不同的错误,我不知道设置它的正确方法,而且由于我还是 React 的新手,我对这些消息中的任何一条都不是很熟悉,我在网上找到的解决方案似乎不起作用。
【问题讨论】:
标签: javascript reactjs testing react-router create-react-app