【问题标题】:React/Mocha Testing: can't find imported componentsReact/Mocha 测试:找不到导入的组件
【发布时间】:2016-08-30 16:00:19
【问题描述】:

我也有类似的问题:

Unable to import a React Component in my Mocha test

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

我在我的调用堆栈(附在底部)中看到,我正在加载 babel-register。

文件如下

文件结构

/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 {

这是我尝试运行 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

其他(可能相关信息)

我正在运行节点版本管理器(节点 4.0),因为出于某种原因,使用较新版本的节点会引发错误。

非常感谢您的帮助!

【问题讨论】:

  • 您能向我们展示您的进口声明吗?
  • 添加到原始问题
  • 导入的from 值表明您有node_modules/components,但您没有。当您不测试代码时,您是否将其与为这些路径起别名的东西捆绑在一起?如果是这样,您将不得不以某种方式通过您的测试设置来解决这个问题。另请参阅stackoverflow.com/questions/36943118/…
  • 完美。我在上面找到了一些资源。没错,我使用的是 webpack 的 resolve: modulesDirectories 来引用它们

标签: reactjs mocha.js babeljs redux


【解决方案1】:

感谢@JMM 帮我隔离源

如果你在 webpack 中使用别名来解析目录路径,你应该将它们包含在你的 mocha 运行脚本中或使用节点模块模拟它们

Using webpack aliases in mocha tests

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多