来自 create-react-app 的笑话不是 运行 Windows
Jest from create-react-app not running on Windows
我有一个问题 运行 来自使用 Windows 8.1 上的 create-react-app
创建的全新安装的应用程序的 Jest。安装后,运行命令npm run test
,出现错误:
No tests found related to files changed since last commit.
运行 jest --watchAll
,因此在同一目录中绕过反应脚本,显示:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
SyntaxError: E:\demo\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 | });
Google 搜索了很长时间,并听从了人们对类似问题的建议,我开始在 package.json 上添加 Babel devDependencies。结果变成了这样:
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.2.3",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@babel/runtime": "^7.2.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.5",
"babel-plugin-root-import": "^6.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react-optimize": "^1.0.1",
"jest-transform-stub": "^1.0.0"
}
我还添加了 .babelrc 文件:
{
"presets": [
"@babel/react" ,
"@babel/env"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
无论我尝试哪种组合,我总是会遇到不同的错误,这是不对的。看看网上的人,所有人都可以使用 create-react-app 开箱即用的 Jest 命令。
最后一次尝试使用所有这些依赖项导致以下错误:
Test suite failed to run
E:\dev\demo\src\App.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
有什么想法吗?!我很困惑。
create-react-app
是生成 react-scripts
预配置设置的 CLI,其中包括 Webpack 和 Jest 配置等。
当Jest直接用作jest
时,它会忽略生成的配置,需要从头开始配置。
应使用 npm test
(在 CRA 设置中默认为 react-scripts test
)而不是 jest
以使用生成的 Jest 配置。
如果 npm test
的问题是交互模式:
No tests found related to files changed since last commit. Press a
to run all tests, or run Jest with --watchAll
可以用npm test a
寻址。
只是为了澄清@estus-flask 的答案。
有 2 个解决方案。
第一个解决方案:
package.json
"scripts": {
"test": "react-scripts test --watchAll",
...
},
然后运行
npm test
第二个解决方案:
或当你运行测试时,运行他们是这样的:
npm test .
就我而言,我是 运行 Azure DevOps 管道,用于构建 React 应用程序并将其部署到 Azure 应用程序服务。所以我使用下面的脚本任务来做npm install,npm build然后测试如下。
- script: |
echo Working dir and file list are as follows.
echo -----------------------------------------
pwd
ls -la
echo Changing directory to reactonazure
ehco ----------------------------------
cd reactonazure
echo Working dir and file list now after chaning directory are as follows.
echo ---------------------------------------------------------------------
pwd
ls -la
echo Running npm install
echo -------------------
npm install
echo Running npm build --if-present
echo ------------------------------
npm run build --if-present
echo Running CI=true npm test
echo ------------------------
CI=true npm test
displayName: 'npm install, build and test'
您可以忽略 echo、ls、pwd 和 cd 语句。重点在最后一个
CI=true npm test
终于开始工作了。早些时候我有
npm run test --if-present
这给了我错误。
No tests found related to files changed since last commit.
我有一个问题 运行 来自使用 Windows 8.1 上的 create-react-app
创建的全新安装的应用程序的 Jest。安装后,运行命令npm run test
,出现错误:
No tests found related to files changed since last commit.
运行 jest --watchAll
,因此在同一目录中绕过反应脚本,显示:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
SyntaxError: E:\demo\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 | });
Google 搜索了很长时间,并听从了人们对类似问题的建议,我开始在 package.json 上添加 Babel devDependencies。结果变成了这样:
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.2.3",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@babel/runtime": "^7.2.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.5",
"babel-plugin-root-import": "^6.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react-optimize": "^1.0.1",
"jest-transform-stub": "^1.0.0"
}
我还添加了 .babelrc 文件:
{
"presets": [
"@babel/react" ,
"@babel/env"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
无论我尝试哪种组合,我总是会遇到不同的错误,这是不对的。看看网上的人,所有人都可以使用 create-react-app 开箱即用的 Jest 命令。
最后一次尝试使用所有这些依赖项导致以下错误:
Test suite failed to run
E:\dev\demo\src\App.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
有什么想法吗?!我很困惑。
create-react-app
是生成 react-scripts
预配置设置的 CLI,其中包括 Webpack 和 Jest 配置等。
当Jest直接用作jest
时,它会忽略生成的配置,需要从头开始配置。
npm test
(在 CRA 设置中默认为 react-scripts test
)而不是 jest
以使用生成的 Jest 配置。
如果 npm test
的问题是交互模式:
No tests found related to files changed since last commit. Press
a
to run all tests, or run Jest with--watchAll
可以用npm test a
寻址。
只是为了澄清@estus-flask 的答案。
有 2 个解决方案。
第一个解决方案:
package.json
"scripts": {
"test": "react-scripts test --watchAll",
...
},
然后运行
npm test
第二个解决方案:
或当你运行测试时,运行他们是这样的:
npm test .
就我而言,我是 运行 Azure DevOps 管道,用于构建 React 应用程序并将其部署到 Azure 应用程序服务。所以我使用下面的脚本任务来做npm install,npm build然后测试如下。
- script: |
echo Working dir and file list are as follows.
echo -----------------------------------------
pwd
ls -la
echo Changing directory to reactonazure
ehco ----------------------------------
cd reactonazure
echo Working dir and file list now after chaning directory are as follows.
echo ---------------------------------------------------------------------
pwd
ls -la
echo Running npm install
echo -------------------
npm install
echo Running npm build --if-present
echo ------------------------------
npm run build --if-present
echo Running CI=true npm test
echo ------------------------
CI=true npm test
displayName: 'npm install, build and test'
您可以忽略 echo、ls、pwd 和 cd 语句。重点在最后一个
CI=true npm test
终于开始工作了。早些时候我有
npm run test --if-present
这给了我错误。
No tests found related to files changed since last commit.