首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在创建-反应-应用程序中使用jest.config.js

如何在创建-反应-应用程序中使用jest.config.js
EN

Stack Overflow用户
提问于 2020-01-23 12:04:26
回答 7查看 42.9K关注 0票数 51

我想将我的jest配置从我的package.json中移出,我试图使用- config作为建议的这里,但是得到了错误argv.config.match is not a function

package.json

代码语言:javascript
运行
复制
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --config jest.config.js",
    "eject": "react-scripts eject",
  },

cli

代码语言:javascript
运行
复制
hutber@hutber-mac:/var/www/management/node$ npm test -u

> management-fresh@0.1.0 test /var/www/management/node
> react-scripts test --config jest.config.js

Usage: test.js [--config=<pathToConfigFile>] [TestPathPattern]


argv.config.match is not a function
npm ERR! Test failed.  See above for more details.
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2020-04-27 21:26:59

对我来说,附加-- --config=jest.config.js起了作用。

所以,在您的情况下,整个字符串react-scripts test -- --config jest.config.js

票数 29
EN

Stack Overflow用户

发布于 2020-08-01 17:30:12

TL;博士

在选项之前添加--

代码语言:javascript
运行
复制
"test": "react-scripts test -- --config=jest.config.js",

这里的问题是react-scripts没有看到传递给它的选项。我们可以通过直接运行它来演示这一点。

代码语言:javascript
运行
复制
./node_modules/.bin/react-scripts test --config=jest.config.js
# argv.config.match is not a function

./node_modules/.bin/react-scripts test -- --config=jest.config.js
# This works.

变体

如何将选项传递给脚本,取决于您使用的、npm、Yarn的哪个版本。为了完整起见,以下是这些变化的结果:

代码语言:javascript
运行
复制
# This runs, but completely ignores the option.
npm test --config=jest.config.js

# These result in "argv.config.match is not a function," indicating that the
# options were not understood.
npm test -- --config=jest.config.js
yarn test -- --config=jest.config.js
yarn test --config=jest.config.js

创建react应用程序package.json中设置测试脚本

代码语言:javascript
运行
复制
"test": "react-scripts test",

您可以像这样设置其他选项。

代码语言:javascript
运行
复制
"test": "react-scripts test -- --config=jest.config.js",

如果您想通过CLI发送选项,这样的操作可能会奏效。

代码语言:javascript
运行
复制
"test": "react-scripts test --",
代码语言:javascript
运行
复制
yarn test --bail
# comes through as
react-scripts test -- --bail

资源

以下是解释不同用法的几个资源。

票数 14
EN

Stack Overflow用户

发布于 2021-09-03 08:25:31

对我来说,在package.json文件中添加package.json作为密钥是有效的。将所有必需的配置添加为jest键而不是jest.config.js中的对象

代码语言:javascript
运行
复制
"jest": {
    "collectCoverageFrom": [
      "src/**/*.js",
      "!**/node_modules/**"
    ],
    "coverageReporters": [
      "text-summary",
      "lcov",
      "cobertura"
    ],
    "testMatch": [
      "**/*.test.js"
    ]
  },
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59878153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档