首页 > 解决方案 > 使用 Jest 进行纱线测试失败:无法从“Form.js”中找到模块“react”

问题描述

我正在开始我的第一个节点/反应项目,我尝试使用 Jest 编写我的第一个测试。

当我使用它们运行它们时yarn test,它会失败并显示以下消息:

Cannot find module 'react' from 'Form.js'

    However, Jest was able to find:
        './App.css'
        './App.js'
        './App.test.js'

    You might want to include a file extension in your import, or update your
 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts',
'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].

我使用 create-react-app 创建了我的项目,这是我的 package.json :

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.1",
    "shards-react": "^1.0.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",

    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "http://localhost:5000"
}

我不明白Form.js文件是什么,因为我没有创建这样的文件。我想我在 package.json 中的依赖项有问题,但我现在找不到解释。

标签: node.jsreactjstestingjestjs

解决方案


推荐阅读