在 mocha 测试中使用 webpack 别名 [英] Using webpack aliases in mocha tests

查看:32
本文介绍了在 mocha 测试中使用 webpack 别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 React/Redux/Webpack 中开发工作中的网络应用程序,现在开始将测试与 Mocha 集成.

I'm developing a web app at work in React/Redux/Webpack and am now starting to integrate testing with Mocha.

我遵循了 Redux 文档中编写测试的说明,但现在我的 webpack 别名遇到了问题.

I followed the instructions for writing tests in the Redux documentation, but now I have run into an issue with my webpack aliases.

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

For example, take a look at the imports section of this test for one of my action creators:

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);
    });
  });
});

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

As the comments suggest, mocha isn't able to resolve my import statements when they are referencing aliased dependencies.

因为我对 webpack 还是个新手,这是我的 webpack.config.js:

Because I'm still new to webpack, here's my 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 中使用的脚本.

Also, I'm using the command npm test to run mocha, here's the script I'm using in my package.json.

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

所以这就是我卡住的地方.我需要在运行时将 webpack 中的别名包含到 mocha 中.

So here's where I get stuck. I need to include the aliases from webpack into mocha when it runs.

推荐答案

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

Okay so I realized that everything I was aliasing was in the src/ directory, so I simply needed to modify my npm run test script.

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

可能不适用于所有人,但这解决了我的问题.

Probably won't work for everyone, but that solved my issue.

这篇关于在 mocha 测试中使用 webpack 别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆