Reactjs React/Mocha测试:can';找不到导入的组件

Reactjs React/Mocha测试:can';找不到导入的组件,reactjs,mocha.js,babeljs,redux,Reactjs,Mocha.js,Babeljs,Redux,我有一个与这个问题类似的问题: 问题是,我已经合并了这两个更改,当我尝试测试组件时,Mocha仍然没有检测到我的“导入”语句 我在我的调用堆栈(附在底部)中看到,我正在加载babel寄存器 文件如下 文件结构 /client /src /components //test components /containers /test /components /dom.js && helper.js

我有一个与这个问题类似的问题:

问题是,我已经合并了这两个更改,当我尝试测试组件时,Mocha仍然没有检测到我的“导入”语句

我在我的调用堆栈(附在底部)中看到,我正在加载babel寄存器

文件如下

文件结构

/client
    /src
      /components
         //test components
      /containers
    /test
      /components
      /dom.js && helper.js
package.json

"babel": {
    "presets": ["es2015"]
  }
"scripts": {
    "start": "node bin/server.js",
    "test": "mocha --compilers js:babel-register \"./test/**/*.js\" --require ignore-styles",
    "test:watch": "npm run test -- --watch"
  },
mocha.opt文件

--require ./test/test_helper.js
--require ./test/dom.js 
--recursive
编辑:从组件文件添加导入语句

//来自组件

import { Modal } from 'components/Modal'
export class RecipeModal extends Component {
//模态分量

import { Modal } from 'components/Modal'
export class RecipeModal extends Component {
这是我尝试运行mocha时收到的调用堆栈/错误消息

Warning: require('react/addons') is deprecated. Access using require('react-addons-{addon}') instead.
Error: Cannot find module 'components/RecipeModal'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:286:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (index.js:11:1)
    at Module._compile (module.js:434:26)

    at loader (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel-register/lib/node.js:168:7)

    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (Recipes_test.js:2:1)
    at Module._compile (module.js:434:26)
    at loader (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel-register/lib/node.js:168:7)

    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at /Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/lib/mocha.js:219:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/lib/mocha.js:216:14)
    at Mocha.run (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/lib/mocha.js:468:10)
    at loadAndRun (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/bin/_mocha:359:22)
    at Object.<anonymous> (/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/bin/_mocha:376:3)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3
警告:不推荐使用require('react/addons')。改为使用require('react-addons-{addon}')进行访问。
错误:找不到模块“组件/RecipeModal”
在Function.Module.\u解析文件名(Module.js:336:15)
在Function.Module.\u加载(Module.js:286:25)
at Module.require(Module.js:365:17)
根据需要(module.js:384:17)
反对。(索引js:11:1)
在模块处编译(Module.js:434:26)
在加载程序中(/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel register/lib/node.js:158:5)
at Object.require.extensions.(匿名函数)[as.js](/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel register/lib/node.js:168:7)
在Module.load(Module.js:355:32)
在Function.Module.\u加载(Module.js:310:12)
at Module.require(Module.js:365:17)
根据需要(module.js:384:17)
反对。(配方试验js:2:1)
在模块处编译(Module.js:434:26)
在加载程序中(/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel register/lib/node.js:158:5)
at Object.require.extensions.(匿名函数)[as.js](/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/babel register/lib/node.js:168:7)
在Module.load(Module.js:355:32)
在Function.Module.\u加载(Module.js:310:12)
at Module.require(Module.js:365:17)
根据需要(module.js:384:17)
at/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach(本机)
在Mocha.loadFiles(/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/Mocha/lib/Mocha.js:216:14)
在Mocha.run(/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/Mocha/lib/Mocha.js:468:10)
加载和运行时(/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/bin/_mocha:359:22)
反对。(/Users/ACKeepingitCoo/Desktop/ketoBot/client/node_modules/mocha/bin/_mocha:376:3)
在模块处编译(Module.js:434:26)
在Object.Module.\u extensions..js(Module.js:452:10)
在Module.load(Module.js:355:32)
在Function.Module.\u加载(Module.js:310:12)
位于Function.Module.runMain(Module.js:475:10)
启动时(node.js:117:18)
在node.js:951:3
其他(可能相关信息)

我正在运行node version manager(node 4.0),因为出于某种原因,使用较新版本的node会引发错误


非常感谢你的帮助

感谢@JMM帮助我隔离源代码

如果您在webpack中使用别名解析目录路径,则应将其包含在mocha运行脚本中,或将其与节点模块一起使用


您能给我们看一下您的进口声明吗?添加到原始问题中,您进口的
值表明您有一个
节点\u模块/组件
,但您没有。当您不测试代码时,是否将其与别名路径绑定在一起?如果是这样的话,你必须用你的测试设置来解释这一点。另见,完美。我在上面找到了一些资源。你是对的,我使用webpack的resolve:modulesDirectory来引用它们