【问题标题】:Running tests with karma and webpack使用 karma 和 webpack 运行测试
【发布时间】:2015-09-11 20:40:11
【问题描述】:

我的项目有外部依赖,所以我这样配置 webpack:

externals:{
        'd3':'d3',
        'another-external-dep': 'another-external-dep'
}

然后在代码中我需要这样的依赖:

var someProp = require('another-external-dep').someProp.

在我整合业力之前一切都很好。 因此,运行测试时 karma 无法清楚地找到模块 another-external-dep,因为它是一个外部依赖项,并且我没有包含在文件列表中的 karma 配置中。

如何模拟 another-external-dep 以便 require('another-external-dep') 返回模拟?还有我可以在哪里指定这个模拟,在配置或模拟中?

【问题讨论】:

  • 您的karma.config.js 文件是什么样的?您应该能够在 webpack 属性中包含外部。至少在理论上,我正在尝试解决同样的问题,但也遇到了麻烦。

标签: karma-runner commonjs webpack


【解决方案1】:

您可以在 karma 测试期间通过在 karma.config.js 中包含 files 数组中的依赖项来链接到外部依赖项。

module.exports = function karmaConfig(config) {
    config.set({
        ...
        files: [
            'path/to/external/jquery.js',
            'tests.webpack.js',
        ],
        webpack: {
            externals: {
                'jquery': 'jQuery',
            },
        },
        ...
    });
};

这使得依赖项在全局上下文中可用,然后您可以从 webpack 的文件中引用它,从而复制您的开发上下文。

【讨论】:

  • 太棒了,这为我的问题指明了正确的方向 +1
猜你喜欢
  • 2019-06-17
  • 2016-12-25
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多