简体   繁体   English

尝试使用 create-react-app 开玩笑地运行打字稿

[英]trying to run typescript with jest with create-react-app

I am tying to setup jest with react having typescript .我想用带有 typescript 的 react 来设置 jest 。
Whenever I run npm test it gives errors.每当我运行npm test它都会出错。

npm test gives me the error below: npm test 给了我以下错误:

Out of the box, Create React App only supports overriding these Jest options:开箱即用,Create React App 仅支持覆盖这些 Jest 选项:

• collectCoverageFrom • coverageReporters • coverageThreshold • collectCoverageFrom •coverageReporters •coverageThreshold
• snapshotSerializers. • 快照序列化程序。

These options in your package.json Jest configuration are not currently supported by Create React App: Create React App 目前不支持 package.json Jest 配置中的这些选项:

• transform • testRegex • moduleFileExtensions • 转换 • testRegex • moduleFileExtensions

If you wish to override other Jest options, you need to eject from the default setup.如果您希望覆盖其他 Jest 选项,则需要从默认设置中弹出。 You can do so by running npm run eject but remember that this is a one-way operation.您可以通过运行 npm run eject 来实现,但请记住,这是一种单向操作。 You may also file an issue with Create React App to discuss supporting more options out of the box.您还可以向 Create React App 提交问题,以讨论支持更多开箱即用的选项。

I don't want to eject, and I also want below options to work我不想弹出,我也希望以下选项起作用
• transform • 变换
• testRegex • 测试正则表达式
• moduleFileExtensions • 模块文件扩展

What should I do??我该怎么办??

my package.json file is as below我的 package.json 文件如下

 {
  "name": "appname",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "jest-cli": "^22.3.0",
    "jest-enzyme": "^4.2.0",
    "react": "^16.2.0",
    "react-scripts": "1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "storybook": "start-storybook -p 9009 -s public",
    "build-storybook": "build-storybook -s public"
  },
  "devDependencies": {
    "@storybook/addon-actions": "^3.3.13",
    "@storybook/addon-links": "^3.3.13",
    "@storybook/addon-storyshots": "^3.3.13",
    "@storybook/addons": "^3.3.13",
    "@storybook/react": "^3.3.13",
    "@types/enzyme": "^3.1.9",
    "@types/jest": "^22.1.2",
    "@types/react-dom": "^16.0.4",
    "@types/storybook__react": "^3.0.7",
    "awesome-typescript-loader": "^3.4.1",
    "babel-core": "^6.26.0",
    "enzyme": "^3.3.0",
    "jest": "^22.3.0",
    "prettier-eslint": "^8.8.1",
    "react-addons-test-utils": "^15.6.2",
    "react-dom": "^16.2.0",
    "redux": "^3.7.2",
    "ts-jest": "^22.0.4",
    "ts-loader": "^3.5.0",
    "typescript": "^2.7.2",
    "typescript-eslint-parser": "^13.0.0"
  },
  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }
}
`

In CRA 2.1 and later versions , you should be able to use TypeScript for your test files without any special jest config at all.在 CRA 2.1 及更高版本中,您应该能够将 TypeScript 用于您的测试文件,而无需任何特殊的 jest 配置。 Just be sure that your __tests__ folder is nested underneath your src folder, or that your test files are inside your src folder and have any of the following extensions:只要确保您的__tests__文件夹嵌套在您的src文件夹下,或者您的测试文件在您的src文件夹中并具有以下任何扩展名:

  • .test.js
  • .test.ts
  • .test.jsx
  • .test.tsx
  • .spec.js
  • .spec.ts
  • .spec.jsx
  • .spec.tsx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 create-react-app 安装 typescript + jest? - How to install typescript + jest with create-react-app? 尝试将 jest 与 create-react-app 和 typescript 一起使用。 获取错误:开玩笑:无法解析 TypeScript 配置文件...找不到模块“ts-node” - Trying to use jest with create-react-app and typescript. Get error: Jest: Failed to parse the TypeScript config file... Cannot find module 'ts-node' create-react-app typescript 不会运行 npm start - create-react-app typescript won't run npm start 创建反应应用程序测试和笑话模拟 - Create-React-App Testing and Jest Mocking create-react-app、代码拆分和玩笑 - create-react-app, code splitting and jest 我正在尝试运行create-react-app命令 - i am trying to run create-react-app command 错误:尝试运行 create-react-app 时出现 memory - Error: out of memory when trying to run create-react-app 使用 create-react-app 通过 JEST 运行测试时,不会显示 TypeScript 错误 - TypeScript errors doesn't show when running tests through JEST with create-react-app 如何让 jest 使用与 create-react-app 上的 react-script 测试相同的配置运行? - How can I make jest run with the same config as react-script test on a create-react-app? 使用typescript将sass加载到create-react-app中 - loading sass in a create-react-app with typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM