将项目添加到 Yarn 工作区时,挂钩调用无效

Bat*_*man 5 javascript jestjs yarnpkg yarn-workspaces

我有一个项目,当我在其中运行测试时,它们都通过了。我已经建立了一个评论库作为一个公共项目,并创建了一个纱线工作区。当我的第一个项目中有一个通用项目,但尚未使用任何组件时,我的多个测试失败了:

\n\n
Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:\n1. You might have mismatching versions of React and the renderer (such as React DOM)\n2. You might be breaking the Rules of Hooks\n3. You might have more than one copy of React in the same app\n
Run Code Online (Sandbox Code Playgroud)\n\n

鉴于我还没有从第二个库导入任何组件,我不明白是什么原因造成的。但仅设置工作区并安装本地依赖项就会导致这些错误。

\n\n

当我在第一个项目中运行 npm ls React 时,我得到以下信息:

\n\n
\xe2\x9d\xaf npm ls react\n@services/first-project@3.0.0 /Users/code/services/first-prject/client\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac enzyme-adapter-react-16@1.15.2\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac react-test-renderer@16.13.1\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 react@16.13.1  extraneous\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\xac react-datetime@2.16.3\n  \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac react-onclickoutside@6.9.0\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 react@16.13.1  extraneous\n
Run Code Online (Sandbox Code Playgroud)\n\n

公共库的package.json:

\n\n
{\n  "name": "@libs/common",\n  "version": "1.0.0",\n  "description": "Shared Component Library for TS Tools",\n  "author": "",\n  "license": "MIT",\n  "repository": "/tools-common",\n  "main": "dist/index.js",\n  "module": "dist/index.modern.js",\n  "source": "src/index.js",\n  "engines": {\n    "node": ">=10"\n  },\n  "scripts": {\n    "build": "microbundle-crl --no-compress --format modern,cjs --css-modules false",\n    "start": "microbundle-crl watch --no-compress --format modern,cjs --css-modules false",\n    "prepublish": "run-s build",\n    "test": "run-s test:unit test:lint test:build",\n    "test:build": "run-s build",\n    "test:lint": "eslint .",\n    "test:unit": "cross-env CI=1 react-scripts test --env=jsdom",\n    "test:watch": "react-scripts test --env=jsdom",\n    "predeploy": "cd example && yarn install && yarn run build",\n    "deploy": "gh-pages -d example/build"\n  },\n  "peerDependencies": {\n    "@ag-grid-community/client-side-row-model": "^23.1.0",\n    "@ag-grid-community/react": "^23.1.0",\n    "@material-ui/core": "^4.9.13",\n    "@material-ui/icons": "^4.9.1",\n    "@material-ui/lab": "^4.0.0-alpha.52",\n    "@material-ui/pickers": "^3.2.10",\n    "gestalt": "1.48.0",\n    "react": "^16.0.0"\n  },\n  "devDependencies": {\n    "@babel/cli": "^7.8.4",\n    "@babel/core": "^7.8.7",\n    "@babel/plugin-proposal-class-properties": "^7.8.3",\n    "@babel/plugin-proposal-object-rest-spread": "^7.8.3",\n    "@babel/plugin-transform-destructuring": "^7.8.3",\n    "@babel/polyfill": "^7.8.7",\n    "@babel/preset-env": "^7.8.7",\n    "@babel/preset-flow": "^7.8.3",\n    "@babel/preset-react": "^7.8.3",\n    "babel-eslint": "^10.0.3",\n    "cross-env": "^7.0.2",\n    "eslint": "^6.8.0",\n    "eslint-config-prettier": "^6.7.0",\n    "eslint-config-standard": "^14.1.0",\n    "eslint-config-standard-react": "^9.2.0",\n    "eslint-plugin-import": "^2.18.2",\n    "eslint-plugin-node": "^11.0.0",\n    "eslint-plugin-prettier": "^3.1.1",\n    "eslint-plugin-promise": "^4.2.1",\n    "eslint-plugin-react": "^7.17.0",\n    "eslint-plugin-standard": "^4.0.1",\n    "gh-pages": "^2.2.0",\n    "microbundle-crl": "^0.13.8",\n    "node-sass": "^4.14.1",\n    "npm-run-all": "^4.1.5",\n    "prettier": "^2.0.4",\n    "react": "^16.13.1",\n    "react-is": "^16.13.1",\n    "react-scripts": "^3.4.1"\n  },\n  "files": [\n    "dist"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我将第一个项目从工作区中取出并安装纱线,则一切正常。我把它放在工作区中,添加通用组件,但它们失败了。

\n