我正在React/Redux/Webpack中开发一个web应用程序,现在开始将测试与Mocha集成.

我跟踪了the instructions for writing tests in the Redux documentation个,但现在我的网页别名出现了问题.

例如,请查看我的一个动作创建者的本测试的导入部分:

import expect       from 'expect'                 // resolves without an issue
import * as actions from 'actions/app';           // can't resolve this alias
import * as types   from 'constants/actionTypes'; // can't resolve this alias

describe('Actions', () => {
  describe('app',() => {
    it('should create an action with a successful connection', () => {

      const host = '***************',
            port = ****,
            db = '******',
            user = '*********',
            pass = '******';

      const action = actions.createConnection(host, port, db, user, pass);

      const expectedAction = {
        type: types.CREATE_CONNECTION,
        status: 'success',
        payload: { host, port, database, username }
      };

      expect(action).toEqual(expectedAction);
    });
  });
});

正如 comments 所暗示的,当我的导入语句引用别名依赖项时,mocha无法解析它们.

因为我对webpack还是新手,下面是我的webpack.config.js:

module.exports = {
  devtool: 'eval-source-map',
  entry: [
    'webpack-hot-middleware/client',
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  resolve: {
    extensions : ['', '.js', '.jsx'],
    alias: {
      actions: path.resolve(__dirname, 'src', 'actions'),
      constants: path.resolve(__dirname, 'src', 'constants'),
      /* more aliases */
    }
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['babel'],
      exclude: /node_modules/,
      include: __dirname
    }]
  }
};

此外,我正在使用命令npm test来运行mocha,下面是我在package.json中使用的脚本.

 {   
   "scripts": {
     "test": "mocha ./src/**/test/spec.js --compilers js:babel-core/register --recursive"
   }
 }

这就是我被卡住的地方.I need to include the aliases from webpack into mocha when it runs.

推荐答案

好的,所以我意识到我的别名都在src/目录中,所以我只需要修改我的npm run test脚本.

{   
  "scripts": {
    "test": "NODE_PATH=./src mocha ./src/**/test/spec.js --compilers js:babel-core/register --recursive"
  }
}

可能不会对每个人都有效,但这解决了我的问题.

Reactjs相关问答推荐

如何使用React防止并发注册并处理Firestore数据库中的插槽可用性

删除表2的收件箱未按预期工作

在带有和设计的表格中禁用和取消选中复选框

在Reaction中单击时,按钮未按预期工作

如何用Reaction做交互式的MathML?

我应该如何管理设置组件初始状态的复杂逻辑?

在动态React组件中删除事件侦听器

更改滑块标记的文本 colored颜色 ./Reaction/MUI 5

带有样式的工具提示导致无法向功能组件提供参考警告

状态改变不会触发重新渲染

React 错误: 只能用作 元素的子元素,从不直接渲染.请将您的 包裹在

如何垂直对齐 React Bootstrap Table 行中的项目

警告:遇到两个子元素拥有同一把 keys .键应该是唯一的,以便组件在更新时保持其身份

如何更改TimePicker中下拉菜单的背景 colored颜色 和字体 colored颜色 - MUI React

需要先填写依赖输入字段,以便其他人工作

类型错误:类型typeof import(js-cookie)上不存在属性get

在复选框中react 检索选中的值

我刚刚部署了我的 Vite React 站点,但我的图标/图像没有部署

需要帮助在 document.queryselector 的 nextjs 页面上插入 useEffect 状态

如果两个组件在 React 中导入相同的 CSS 文件会发生什么?