Set*_*ske 5 testing unit-testing reactjs jestjs
我正在尝试为我正在制作的库编写一些测试。这不是一个 creat-react-app 项目,而是用 babel 从头开始构建的项目。我正在使用jest,我遇到了这个问题。我看到了类似的问题,但没有像这样的。"test": "jest"我的 package.json 脚本中有。__tests__我的根文件夹中有一个目录。我的测试文件之一名为Component.js,以一些简单的导入开始:
import React from 'react';
import { MyComponent } from '../src/MyComponent.js';
Run Code Online (Sandbox Code Playgroud)
当我运行时npm run test,我收到此错误:
Cannot find module 'react' from '__tests__/MyComponent.js'
> 1 | import React from 'react';
Run Code Online (Sandbox Code Playgroud)
即使我注释掉反应导入,我也会收到此错误:
Cannot find module 'react' from 'src/MyComponent.js'
Require stack:
src/MyComponent.js
__tests__/MyComponent.js
> 1 | import React from 'react';
Run Code Online (Sandbox Code Playgroud)
这种情况不仅发生在 React 中,而且发生在测试中导入的任何节点模块或测试导入的任何模块中。我不明白。jest不知道在node_modules文件夹中寻找这些模块吗?我的 package.json 中没有任何 jest.config.js 或任何jest属性 - 我认为我不需要指定 jest 应该在node_moduldes文件夹中查找这些模块。我是否需要为 jest 设置一些配置才能在 node_modules 中查找这些类型的导入?我觉得这里有一个简单的修复方法在暗示我。谢谢阅读。
编辑:摩卡给了我类似的问题
我想我会尝试使用摩卡来绕过整个问题。使用 的测试脚本"mocha": "mocha --require @babel/register '**/__tests__/*' -R spec",我收到类似的错误:
Error: Cannot find module 'react'
Require stack:
- /Users/seth/Documents/GitHub/react-esri-leaflet/__tests__/MyComponent.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/esm-utils.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/mocha.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/cli/one-and-dones.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/cli/options.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:713:15)
Run Code Online (Sandbox Code Playgroud)
这到底是怎么回事?这些测试库不应该知道在我的 rootFolder/node_modules 中查找这些东西吗?我的 package.json 或(现已过时)webpack.config.js 文件中的某些内容可能会产生干扰吗?您无需将这些文件复制到本文中,而是可以在此处的存储库中查看它们以及整个项目目录: https: //github.com/slutske22/react-esri-leaflet
| 归档时间: |
|
| 查看次数: |
11343 次 |
| 最近记录: |